0% found this document useful (0 votes)
35 views

FDS 4

The document contains code for a student enrollment program. It defines functions to accept student roll numbers, print the roll numbers, sort the numbers using insertion sort, and perform non-recursive and recursive ternary searches on the sorted list to find specific roll numbers. The main section runs a menu loop allowing the user to call these functions and exit the program.

Uploaded by

Yashwant Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

FDS 4

The document contains code for a student enrollment program. It defines functions to accept student roll numbers, print the roll numbers, sort the numbers using insertion sort, and perform non-recursive and recursive ternary searches on the sorted list to find specific roll numbers. The main section runs a menu loop allowing the user to call these functions and exit the program.

Uploaded by

Yashwant Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name-Madhusudan Ramchandra Vetal.

Class-S. E.
Div-C
Roll No. – COSC65
Batch-C3

Program Code:

def
accept_roll()
:
roll_no = []
no_of_students = int(input("Enter the number of students : "))
for i in range(no_of_students):
roll_no.append(int(input("Enter Roll Number of Student {0} :
".format(i+1))))
return roll_no

#<------------------------------------------------------------------------
--------------------->

# Printing the Roll Numbers of the Students

def print_roll(roll_no):
for i in range(len(roll_no)):
print(roll_no[i],sep = "\n")

#<------------------------------------------------------------------------
--------------------->
# Insertion Sort for Sorting the list of Roll Numbers

def insertion_sort(roll_no):
for i in range(1,len(roll_no)):
key = roll_no[i]
j = i-1;
while j >= 0 and key < roll_no[j]:
roll_no[j+1] = roll_no[j]
j -= 1
roll_no[j+1] = key
return roll_no

#<------------------------------------------------------------------------
---------------------->

# Function for performing Non-Recursive Ternary Search

def NR_Ternary_Search(roll,roll_find):
left = 0
right = len(roll) - 1
while left <= right:
mid1 = left + (right - left) // 3
mid2 = left + 2 * (right - left) // 3
if roll_find == roll[left]:
return left
elif roll_find == roll[right]:
return right
elif roll_find < roll[left] or roll_find > roll[right]:
return -1
elif roll_find <= roll[mid1]:
right = mid1
elif roll_find > roll[mid1] and roll_find <= roll[mid2]:
left = mid1 + 1
right = mid2
else:
left = mid2 + 1
return -1
#<------------------------------------------------------------------------
------------------------->

# Function for performing Recursive Ternary Search

def R_Ternary_Search(roll, left, right, roll_find):


if (right >= left):
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if (roll[mid1] == roll_find):
return mid1
if (roll[mid2] == roll_find):
return mid2

if (roll_find < roll[mid1]):


return R_Ternary_Search(roll, left, mid1 - 1, roll_find)
elif (roll_find > roll[mid2]):
return R_Ternary_Search(roll, mid2 + 1, right, roll_find)
else:
return R_Ternary_Search(roll, mid1 + 1, mid2 - 1, roll_find)
return -1

#<------------------------------------------------------------------------
---------------------------->

# Main
unsort_Roll = []
sort_Roll = []
flag = 1

while flag == 1:
print("\n---------------------MENU---------------------")
print("1. Accept Student Roll Numbers")
print("2. Display the Roll Numbers of Student")
print("3. Sort Roll Numbers from the list")
print("4. Perform Non-Recursive Ternary Search")
print("5. Perform Recursive Ternary Search")
print("6. Exit\n")

ch = int(input("Enter your choice (from 1 to 6) : "))

if ch == 1:
unsort_Roll = accept_roll()

elif ch == 2:
print_roll(unsort_Roll)

elif ch == 3:
print("Elements after performing Insertion Sort : \n")
sort_Roll = insertion_sort(unsort_Roll)
print_roll(sort_Roll)

elif ch == 4:
find_roll = int(input("Enter the Roll Number to be searched :
"))
index = NR_Ternary_Search(sort_Roll,find_roll)
if index != -1:
print("The Roll Number",find_roll,"is found at
position",index+1)
else:
print("Roll Number",find_roll,"nor found!!")

elif ch == 5:
find_roll = int(input("Enter the Roll Number to be searched :
"))
left = 0
right = len(sort_Roll) - 1
index = R_Ternary_Search(sort_Roll,left,right,find_roll)
if index != -1:
print("The Roll Number",find_roll,"is found at
position",index+1)
else:
print("Roll Number",find_roll,"nor found!!")

elif ch == 6:
print("Thanks for using this program!!")
flag=0

else:
print("Wrong choice!!")
flag = 0

#<----------------------END OF PROGRAM------------------------->

[10, 10, 10]

[10, 10, 10]O Output


[10, 10, 10]
1 : Accept & Display Students info
2 : Linear Search
3 : Sentinel Search
4 : Exit
Enter your choice :
Error: Command failed: timeout 7 python3 HelloWorld.py

[10, 10, 10]


[10, 10, 10]
[10, 10, 10]

[10, 10, 10]

OuO[10, 10, 10] Output


[10, 10, 10]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy