#------------------------------------------------------------------------------- # BubbleSort.py # Sorts any list whose elements are comparable #------------------------------------------------------------------------------- def swap(L, i, j): temp = L[i] L[i] = L[j] L[j] = temp # end of swap() def sort(L): # implements the Bubble Sort algorithm for i in range(len(L)-1,0,-1): for j in range(i): if L[j]>L[j+1]: swap(L, j, j+1) # end if #print(" ", L) # end inner for # print() #print(L) # end outer for # end of function sort() # function main() ------------------------------------------------------------- def main(): A = list(range(100,0,-1)) #A = [8,2,9,3,5,-34,67] #A = [4, 3, 5, 1, 2] #A = [5, 4, 3, 2, 1] print('\nbefore:') print(A) print() sort(A) print('\nafter:') print(A) print() # end main() ------------------------------------------------------------------ # closing conditional --------------------------------------------------------- if __name__=='__main__': main() # end