You are on page 1of 4

DSA LAB TASK 4

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("*original Array* ")

print(array)

print()

print("bubble sort : ")

for a in range(len(array)-1):

flag = False

for b in range(len(array)-1-a):

if array[b] > array [b+1]:

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()

print("number of swapping : ",count)

#selection sort

print()

array = np.array([4,3,9,3,1])

count = 0

print("original length : ")

print(array)

print("Selection sort : ")

for a in range(len(array)-1):

index = a

for b in range(a+1,len(array)):

if array[b] < array[index]:

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()

print("number of swapping : ",count)

#insertion

print()

array = np.array([4,3,9,3,1])

count = 0

print("original length : ")

print(array)

print("Insertion sort : ")

for a in range(1,len(array)):

temp = array[a]

index = a

while(index!=0 and array[index-1] > temp):

array[index] = array[index-1]

index-=1

array[index] = temp

count+=1

for a in array:

print(a,end=" ")

print()

print("number of swapping : ",count)


OUTPUT SCREENSHOT:

You might also like