0% found this document useful (0 votes)
5 views6 pages

Def LinearSearch

Uploaded by

kadamaaryak28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Def LinearSearch

Uploaded by

kadamaaryak28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

def LinearSearch(arr, key, count):

found = False
position = -1
index = 0
while index < count and found == False:
if key == arr[index]:
found = True
position = index
break
index += 1
return position

def SentinalSearch(arr, x, n):


last = arr[n-1]
arr[n-1] = x
i=0
while arr[i] != x:
i += 1
arr[n-1] = last
if i < n-1 or x == arr[n-1]:
print(x,"is present at index",i)
else:
print("Not Found")

# Using Recursion
def BinarySearch(arr, x, count, low, high):
if low <= high:
mid = (low + high)//2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
return BinarySearch(arr, x, count, low ,high)
else:
high = mid - 1
return BinarySearch(arr, x, count, low ,high)
else:
return -1

def Fibo(n):
if n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return Fibo(n-1) + Fibo(n-2)
def FibonacciSearch(arr, x, N):
m=0
while Fibo(m) < N:
m += 1
offset = -1
while Fibo(m) > 1:
mid = min(offset + Fibo(m-2), N-1)
if x > arr[mid]:
m -= 1
offset = mid
elif x < arr[mid]:
m -= 2
else:
return mid
if Fibo(m-1) == False and arr[offset + 1] == x:
return offset + 1
return -1

# count = int(input("Enter Number of students"))


count = 10
students = [10, 8, 35, 51, 27, 50, 49, 4, 69, 34]
num =49

print("""
Please select your sort type:
1. Linear search
2. Sentinel Search
3. Binary Search
4.Fibonacci Search
0. Exit
""")

while True:
sec = input("Select your section : ")
if sec == '1':
p = LinearSearch(students, num, count)
if p == -1:
print("Not found")
else:
print("Student",num,"found at positon",p)
elif sec == '2':
SentinalSearch(students, num, count)
elif sec == '3':
students.sort()
low = 1
high = count
p = BinarySearch(students, num, count, low, high)
if p == -1:
print("Not found")
else:
print("Student",num,"found at positon",p)
elif sec == '4':
students.sort()
p = FibonacciSearch(students, num, count)
if p == -1:
print("Not found")
else:
print("Student",num,"found at positon",p)
elif sec == '0':
print("Thank You, Programme Endded")
break
else:
print("Enter vaild selection")
OUTPUT:

C:\Users\dryog\AppData\Local\Programs\Python\Python39\python.exe
C:/Users/dryog/Downloads/Assignment4.py

Please select your sort type:

1. Linear search

2. Sentinel Search

3. Binary Search

4.Fibonacci Search

0. Exit

Select your section : 1

Student 49 found at positon 6

Select your section : 2

49 is present at index 6

Select your section : 3

Student 49 found at positon 6

Select your section : 4

Student 49 found at positon 6

Select your section : 5

Enter vaild selection


''' Write a Python program to store first year percentage of students in array. Write
function for sorting array of floating point numbers in ascending order using quick sort
and display top five scores. '''

class sort:
def partition(self,arr,start,end):
pivot=arr[end]
i=start-1
for j in range(start, end):
if(arr[j]<=pivot):
i+=1
arr[i],arr[j]=arr[j],arr[i]
arr[i+1],arr[end]=arr[end],arr[i+1]
return i+1

def quick_sort(self,arr,start,end):
if(start<end):
p=self.partition(arr,start,end)
self.quick_sort(arr,start,p-1)
self.quick_sort(arr,p+1,end)

s=sort()
def arrinput():
marks=[]
a=int(input("Enter total number of students - "))
for i in range(a):
c=float(input("enter percentage - "))
marks.append(c)
print("unsorted list of percentages - ",marks)
print("--------------------------------------------------------------------------")
x=len(marks)-1
y=0
s.quick_sort(marks,y,x)
print("sorted list of percentages - ",marks)
print("--------------------------------------------------------------------------")
print("Top five scores are: ")
for i in range(len(marks)):
print(marks[i])
print("--------------------------------------------------------------------------")

arrinput()
C:\Users\dryog\AppData\Local\Programs\Python\Python39\python.exe
C:/Users/dryog/Downloads/a6.py

Enter total number of students - 5

enter percentage - 70

enter percentage - 60

enter percentage - 55

enter percentage - 98

enter percentage - 12

unsorted list of percentages - [70.0, 60.0, 55.0, 98.0, 12.0]

--------------------------------------------------------------------------

sorted list of percentages - [12.0, 55.0, 60.0, 70.0, 98.0]

--------------------------------------------------------------------------

Top five scores are:

12.0

55.0

60.0

70.0

98.0

--------------------------------------------------------------------------

Process finished with exit code 0

You might also like