Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment Title: 3.

1
Student Name: Pritam Kumar Dutta UID: 20BCS3296
Branch: CSE Section/Group: 620-B
Semester: 4 Date of Performance: April 21, 2022

➢ AIM:
 Python program to implement linear search.
 Python program to implement bubble sort.
 Python program to implement binary search without recursion.
 Python program to implement selection sort.

➢ Program Code:
def linear_search(arr,x):
pos=0
for i in range(len(arr)):
if arr[i]==x:
pos=i+1
if(pos!=0):
print(x," is found within the list at position no.",pos)
else:
print(x,"not found!")

def bubble_sort(arr,o):
n=len(arr)
if o==1:
for i in range(n-1):
for j in range(n-i-1):
if arr[j]>arr[j+1]:
t=arr[j];arr[j]=arr[j+1];arr[j+1]=t
else:
for i in range(n-1):
for j in range(n-i-1):
if arr[j]<arr[j+1]:
t=arr[j];arr[j]=arr[j+1];arr[j+1]=t
print("\nThe sorted list is: ",arr)

def binary_search(arr,x):
n=len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j]>arr[j+1]:
t=arr[j];arr[j]=arr[j+1];arr[j+1]=t
print("Sorted list:",arr)
low=0;high=n-1;mid=0;pos=0
while (low<=high):
mid=((high+low)//2)
if arr[mid]<x:
low=mid+1
elif arr[mid]>x:
high=mid-1
else:
pos=mid+1
break
if(pos!=0):
print(x," is found within the list at position no.",pos)
else:
print(x,"not found!")

def selection_sort(arr,o):
n=len(arr)
for i in range(n):
mami=i
for j in range(i+1,n):
if o==1:
if arr[j]<arr[mami]:
mami=j
else:
if arr[j]>arr[mami]:
mami=j
t=arr[i];arr[i]=arr[mami];arr[mami]=t
print("\nThe sorted list is: ",arr)

c=0
while(c!=2):
n=int(input("\nEnter size of list: "))
a=[]
print("Enter list elements: ")
for i in range(n):
x=int(input())
a.append(x)
k=0
while(k!=5):
k=int(input("\nChoose action: 1. Linear Search 2. Bubble Sort 3. Binary Search 4.
Selection Sort 5. Exit\n"))
if k==1:
print("\nLinear Search: ")
x=int(input("Enter the element to find: "))
linear_search(a,x)
elif k==2:
print("\nBubble Sort: ")
o=int(input("Choose: 1.Ascending Order 2.Descending order "))
bubble_sort(a,o)
elif k==3:
print("\nBinary Search: ")
x=int(input("Enter the element to find: "))
binary_search(a,x)
elif k==4:
print("\nSelection Sort: ")
o=int(input("Choose: 1.Ascending Order 2.Descending order "))
selection_sort(a,o)
elif k==5:
break
else:
print("\nErRoR! Invalid Option!!")
c=int(input("\nDo you wish to try again? 1.Yes 2.No "))

print("\n\n\nThanks~~~~~created by: Pritam_3296")


➢ Output:

You might also like