Professional Documents
Culture Documents
Assi 11
Assi 11
Assi 11
training program in random order. Write function for searching whether particular
student attended training program or not, using Linear search and Sentinel search.
b) Write a python program to store roll numbers of student array who attended training
program in sorted order. Write function for searching whether particular student
attended training program or not, using Binary search and Fibonacci search
Program:
print("Part A")
print("linear Search")
tot=int(input("Enter the number of students: "))
list1=[]
for i in range(tot):
roll_num=int(input("Enter the roll number: "))
list1.append(roll_num)
print(list1)
def search(list1,tot,sear_num):
for i in range(0,tot):
if(list1[i]==sear_num):
return i
return -1
result=search(list1,tot,sear_num)
if result==-1:
print("Element not found")
else:
print("Element found at location",result)
print("*****************************************")
print("Sentinel Search")
li=[]
tot1=int(input("Enter the number of students: "))
for i in range(tot1):
roll=int(input("Enter the roll number: "))
li.append(roll)
print(li)
sear=int(input("Enter the roll number of student to be
searched:"))
def sentinel_search(li,sear):
size=len(li)
li.append(sear)
i=0
while(li[i]!=sear):
i=i+1
if(i==size):
return None
else:
return i
print(f"{sear} is present at index: ",sentinel_search(li,sear))
print("*****************************************")
print("Part b")
print("Binary Search")
print("Binary Search")
lis=[]
num_ele=int(input("Enter the number of students: "))
for i in range(num_ele):
ele=int(input("Enter the roll number: "))
lis.append(ele)
print(lis)
a=len(lis)
x=int(input("Enter the roll number to be searched:"))
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list")
Output:
Part A
linear Search
Enter the number of students: 4
Enter the roll number: 22
Enter the roll number: 33
Enter the roll number: 44
Enter the roll number: 55
[22, 33, 44, 55]
Enter the roll number of student to be searched:55
Element found at location 3
*****************************************
Sentinel Search
Enter the number of students: 4
Enter the roll number: 22
Enter the roll number: 33
Enter the roll number: 44
Enter the roll number: 55
[22, 33, 44, 55]
Enter the roll number of student to be searched:55
55 is present at index: 3
*****************************************
Part b
Binary Search
Binary Search
Enter the number of students: 4
Enter the roll number: 22
Enter the roll number: 33
Enter the roll number: 44
Enter the roll number: 55
[22, 33, 44, 55]
Enter the roll number to be searched:55
Element is present at index 3