Join 36000+ teachers and students using TTIO.
Bubble sort is a comparison?-based algorithm that compares each pair of elements in an array and swaps them if they are out of order until the entire array is sorted.
#1 Write a Python program to sort a list of elements using the bubble sort algorithm
#2 Comment each line of the python solution below to show your understanding of the algorithm
def bubbleSort(nlist): for passnum in range(len(nlist)-1,0,-1): for i in range(passnum): if nlist[i]>nlist[i+1]: temp = nlist[i] nlist[i] = nlist[i+1] nlist[i+1] = temp nlist = [14,46,43,27,57,41,45,21,70] bubbleSort(nlist) print(nlist)
www.teachyourselfpython.com