fork download
  1. # your code goes here
  2. # your code goes here
  3. def bubbleSort(numList):
  4. '''
  5. Takes a list and returns 2 values
  6. 1st returned value: a dictionary with the state of the list after each complete pass of bubble sort
  7. 2nd returned value: the sorted list
  8.  
  9. >>> bubbleSort([9,3,5,4,1,67,78])
  10. ({1: [3, 5, 4, 1, 9, 67, 78], 2: [3, 4, 1, 5, 9, 67, 78], 3: [3, 1, 4, 5, 9, 67, 78], 4: [1, 3, 4, 5, 9, 67, 78], 5: [1, 3, 4, 5, 9, 67, 78]}, [1, 3, 4, 5, 9, 67, 78])
  11. '''
  12. # initialization of dictionary and loop counter
  13. bubble_dict = {}
  14. num_loop = 0
  15. # for loop that resets swapped to False and adds 1 to num_loop
  16. for i in range(len(numList) - 1):
  17. swapped = False
  18. num_loop += 1
  19. # runs through the list index, swaps index with index+1 if appropriate
  20. for j in range(len(numList) - 1):
  21. if numList[j] > numList[j+1]:
  22. temp = numList[j]
  23. numList[j] = numList[j+1]
  24. numList[j+1] = temp
  25. swapped = True
  26. # Everything works up to adding to the dictionary, even printouts
  27. # of num_loop and numList
  28. print(num_loop, numList)
  29. bubble_dict[num_loop] = numList
  30. if not swapped:
  31. break
  32.  
  33. return bubble_dict, numList
  34.  
  35.  
  36. print( bubbleSort([9,3,5,4,1,67,78]))
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
1 [3, 5, 4, 1, 9, 67, 78]
2 [3, 4, 1, 5, 9, 67, 78]
3 [3, 1, 4, 5, 9, 67, 78]
4 [1, 3, 4, 5, 9, 67, 78]
5 [1, 3, 4, 5, 9, 67, 78]
({1: [1, 3, 4, 5, 9, 67, 78], 2: [1, 3, 4, 5, 9, 67, 78], 3: [1, 3, 4, 5, 9, 67, 78], 4: [1, 3, 4, 5, 9, 67, 78], 5: [1, 3, 4, 5, 9, 67, 78]}, [1, 3, 4, 5, 9, 67, 78])