#--------------------------------------------------- # Function name: bubbleSort # Input: an array, list # Output: the sorted array, list # Note: For simplicity, this version of bubble sort performs exactly (n-1) passes. # It does not end early if data is already ordered. def bubbleSort (list): n = len(list) # number of elements in list passNum = 1 # start with the 1st pass while (passNum < n): index = 0 while (index < (n-passNum)): if (list[index] > list[index+1]): temp = list[index+1] list[index+1] = list[index] list[index] = temp index = index + 1 passNum = passNum + 1 return list #--------------------------------------------------- # Main program: # students = ["Steve", "Garrett", "Danyu", "Krystal", "Chau", "Winnie", "Peter"] print print "Initial order in list is:" print " ", students # display initial order in list print students = ["Steve", "Garrett", "Danyu", "Krystal", "Chau", "Winnie", "Peter"] students = bubbleSort (students) print print "Final order in list is:" print " ", students # display initial order in list print