You are on page 1of 4

1) Linear Search

Main.py
print("Enter the total number of tickets:")
n=int(input())
p=[]
for i in range(n):

p.append(int(input("Enter the ticket number %d:\n"%(i+1))))


print("The ticket numbers are:\n")
for i in range(n):
print(p[i],end=" ")
print("\nEnter the ticket number to be searched:\n")
b=int(input())
flag = 0
for i in range(n):
if(p[i]==b):
print("The ticket number "+str(b)+" is found at position",i+1)
flag=1
break
if(flag==1):
print("Congratulations!You have won the lottery")
else:
print("Sorry the ticket number "+str(b)+" is not there.Better luck next time!")

2) Binary Search
Main.py
def binarySearch (lis, l, r, key):
if r >= l:
mid = l + (r - l) // 2
if lis[mid] == key:
return mid
elif lis[mid] > key:
return binarySearch(lis, l, mid-1, key)
else:
return binarySearch(lis, mid + 1, r, key)
else:
return -1
n = int(input("Enter the total number of jails in Humayun's Place:\n"))
lis,k = [],1
while n!=0:
print("Enter the jail number",k)
data = int(input())
lis.append(data)
k+=1
n-=1
key = int(input("Enter the clue given by Humayun:\n"))
if binarySearch(lis, 0, len(lis)-1, key) != -1:
print("Hurray!The King rescued the Queen")
else:
print("The King has been fooled by Humayun")

3) Bubble Sort Algorithm


Main.py
def printarry(li):
for i in li:
print(i,end=" ")
print()
n=int(input("How many students can be seated in a bench?\n"))
l,k=[],1
for i in range(n):
print("Enter the height of student",k)
data=int(input())
l.append(data)
k+=1
print("Height order of students before sorting:")
printarry(l)
for i in range(len(l)-1):
b=False
for j in range(len(l)-i-1):
if(l[j]>l[j+1]):
l[j+1],l[j]=l[j],l[j+1]
b=True
print("Height order of students after iteration",(i+1))
printarry(l)
if b==False:
break
print("Final sorting of students in a bench are:")
printarry(l)

4) INSERTION SORT
Main.py
def insertionSort(l):
for i in l:
print(i,end=" ")
print()
n=int(input("Enter the total number of cards\n"))
l,x=[],1
for i in range(n):
print("Enter card number",x)
data=int(input())
l.append(data)
x+=1
print("Cards before being sorted:")
insertionSort(l)
for i in range(1,len(l)):
for j in range(i):
if(l[j]>l[i]):
temp=l[i]
for k in range(i,j,-1):
l[k]=l[k-1]
l[j]=temp
print("Arrangement of cards after iteration",i)
insertionSort(l)
print("Finally the sorted card order is:")
insertionSort(l)

5) SELECTION SORT
Main.py
def selectionSort(l):
for i in l:
print(i,end=" ")
print()
n=int(input("Enter the total number of students in class\n"))
l,x=[],1
for i in range(n):
print("Enter the height of student",x)
data=int(input())
l.append(data)
x+=1
print("Student's height order before sorting:")
selectionSort(l)
for i in range(len(l)):
for j in range(i,len(l)):
if(l[j]<l[i]):
l[i],l[j]=l[j],l[i]
print("Height order of students after iteration",(i+1))
selectionSort(l)
print("After final comparison of all students, the height order becomes:")
selectionSort(l)

6) MERGE SORT
Main.py
def mergeSort(list1, left_index, right_index):
if left_index >= right_index:
return
middle = (left_index + right_index)//2
mergeSort(list1, left_index, middle)
mergeSort(list1, middle + 1, right_index)
merge(list1, left_index, right_index, middle)
print("Sub-Merged Value:",end="")
for i in range(left_index,right_index+1):
print(list1[i],end=" ")
print()
def merge(list1, left_index, right_index, middle):
left_sublist = list1[left_index:middle + 1]
right_sublist = list1[middle+1:right_index+1]
left_sublist_index = 0
right_sublist_index = 0
sorted_index = left_index
while left_sublist_index < len(left_sublist) and right_sublist_index <
len(right_sublist):
if left_sublist[left_sublist_index] <= right_sublist[right_sublist_index]:

list1[sorted_index] = left_sublist[left_sublist_index]
left_sublist_index = left_sublist_index + 1
else:
list1[sorted_index] = right_sublist[right_sublist_index]
right_sublist_index = right_sublist_index + 1
sorted_index = sorted_index + 1
while left_sublist_index < len(left_sublist):
list1[sorted_index] = left_sublist[left_sublist_index]
left_sublist_index = left_sublist_index + 1
sorted_index = sorted_index + 1

while right_sublist_index < len(right_sublist):


list1[sorted_index] = right_sublist[right_sublist_index]
right_sublist_index = right_sublist_index + 1
sorted_index = sorted_index + 1
list1 = []
n=int(input("Enter total no. of elements:"))
print("\nEnter",n,"elements:",end="")
for i in range(n):
list1.append(int(input()))
print()
mergeSort(list1, 0, len(list1) -1)
print("Merge sorted elements:",end="")
for i in list1:
print(i,end=" ")

7) Quick Sort

Main.py
def partition(lis, low, high):
i = (low-1)
pivot = lis[high]

for j in range(low, high):


if lis[j] <= pivot:
i = i+1
lis[i], lis[j] = lis[j], lis[i]

lis[i+1], lis[high] = lis[high], lis[i+1]


return (i+1)
def quickSort(lis, low, high):
if len(lis) == 1:
return lis
if low < high:
p = partition(lis, low, high)
quickSort(lis, low, p-1)
quickSort(lis, p+1, high)
n = int(input("Enter the number of elements:\n"))
print("Enter the elements:")
lis=[]
while n!=0:
a = int(input())
lis.append(a)
n-=1
l = len(lis)
quickSort(lis, 0, l-1)
print("Sorted array: ")
for i in lis:
print(i,end=" ")

You might also like