In: Computer Science
ANSWER:
def selectionSort(l):
for i in range(len(l)-1):
print("Pass",i+1,"for Selection Sort:",l)
index=i
for j in range(i+1,len(l)):
if l[index] > l[j]:
index = j
l[i],l[index] = l[index],l[i]
print("Final Sorted Array:",l)
def insertionSort(l):
for i in range(1, len(l)):
element = l[i]
j = i-1
print("Pass",i,"for Insertion Sort:",l)
while(j >= 0 and element < l[j]):
l[j + 1] = l[j]
j -= 1
l[j + 1] = element
print("Final Sorted Array:",l)
def bubbleSort(l):
n= len(l)
for i in range(n-1):
print("Pass",i+1,"for Bubble Sort:",l)
for j in range(0, n-i-1):
if(l[j] > l[j+1]):
l[j],l[j+1] = l[j+1],l[j]
print("Final Sorted Array:",l)
l= [-1, 20, 10, -5, 0, -7, 100, -7, 30, -10]
selectionSort(list(l))
print("-------------------------------------")
insertionSort(list(l))
print("-------------------------------------")
bubbleSort(list(l))
NOTE: The above code is in Python3. Please refer to the attached screenshots for code indentation and sample I/O.
SAMPLE I/O: