# File: bubble.py # # Testing the running time of Bubble Sort # import time import random #REPS = 4000 REPS = 4000 # Create a list of random numbers # def initRandom(): global REPS A = [] for i in range(0,REPS): A.append(random.randint(1,5*REPS)) return A # Checks if a list is sorted # def checkSorted(A): if len(A) <= 1: return TRUE previous = A[0] for i in range(1,len(A)): if previous > A[i]: return False previous = A[i] return True # Use Bubble Sort to sort a list. # def bubbleSort(A): n = len(A) for i in range(1, n): for j in range(0, n-i): if A[j] > A[j+1]: A[j], A[j+1] = A[j+1], A[j] #swap def main(): random.seed() aList = initRandom() # print(aList) start = time.time() bubbleSort(aList) if not checkSorted(aList): print("List not sorted!") finish = time.time() print("Elapsed time =", finish - start, "seconds") # print(aList) main()