Assi 11

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

a) Write a python program to store roll numbers of student in array who attended

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)

sear_num=int(input("Enter the roll number of student to be


searched:"))

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:"))

def binary_search(lis, x):


low = 0
high = len(lis) - 1
mid = 0

while low <= high:


# for get integer result
mid = (high + low) // 2

# Check if n is present at mid


if lis[mid] < x:
low = mid + 1

# If n is greater, compare to the right of mid


elif lis[mid] > x:
high = mid - 1

# If n is smaller, compared to the left of mid


else:
return mid

# element was not present in the list, return -1


return -1
result = binary_search(lis, x)

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

Process finished with exit code 0

You might also like