Professional Documents
Culture Documents
DSA LAB TASK 4-Shaloom-11438
DSA LAB TASK 4-Shaloom-11438
SHALOOM PAUL
SID: 11438
CID: 106287
QUESTION 1:
Implement Bubble sorting, Selection sorting and Insertion sorting on user defined array. Show step
by step simulation of the algorithm and compare and find the best searching algorithm for the
given value.
4, 3, 9, 3, 1
-----------------------------------------------------------------------------------------------------------------
CODING:
import numpy as np
#bubble sort
array = np.array([4,3,9,3,1])
count = 0
print(array)
print()
for a in range(len(array)-1):
flag = False
for b in range(len(array)-1-a):
temp = array[b]
array[b] = array[b+1]
array[b+1] = temp
flag = True
count +=1
if(not flag):
break
for a in array:
print(a,end=" ")
print()
#selection sort
print()
array = np.array([4,3,9,3,1])
count = 0
print(array)
for a in range(len(array)-1):
index = a
for b in range(a+1,len(array)):
index = b
if array[a] != array[index]:
temp = array[a]
array[a] = array[index]
array[index] = temp
count +=1
for a in array:
print(a,end=" ")
print()
#insertion
print()
array = np.array([4,3,9,3,1])
count = 0
print(array)
for a in range(1,len(array)):
temp = array[a]
index = a
array[index] = array[index-1]
index-=1
array[index] = temp
count+=1
for a in array:
print(a,end=" ")
print()