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

DSL 4

Uploaded by

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

DSL 4

Uploaded by

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

Source Code (Linear Search):

A = []
a=int(input("Enter no. of present students : "))
for i in range (a):
pre =int(input("Enter roll no. of present students : "))
A.append(pre)
print("Present Students are : ",A)
b=int(input("Enter roll no. you want to search : "))
if b not in A:
print("Absent")
else:
print("Present")

Output:
Enter no. of present students : 2
Enter roll no. of present students : 1
Enter roll no. of present students : 2
Present Students are : [1, 2,]
Enter roll no. you want to search : 7
Absent

Source Code (Sentinel Search):


A = []
a=int(input("Enter no. of present students : "))
for i in range (a):
pre =int(input("Enter roll no. of present students : "))
A.append(pre)
print("Present Students are : ",A)
key=int(input("Enter roll no. you want to search : "))
temp = A[-1]
A[-1]=key
c=0
while(A[c]!=key):
c+=1
A[-1]=temp
if c<(len(A)-1) or A[-1]==key:
print("Present")
else:
print("Absent")

Output:
Enter no. of present students : 2
Enter roll no. of present students : 1
Enter roll no. of present students : 2
Present Students are : [1, 2]
Enter roll no. you want to search : 2
Present

You might also like