Advertisement
brendan-stanford

sort_comparison_python3

Aug 24th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. from timeit import timeit
  2. from random import randint
  3.  
  4. '''
  5. BUBBLE SORT START
  6. '''
  7. #This program takes an unsorted list and sorts it, then stores the results in copy_list
  8.  
  9.  
  10. #my_list becomes filled with a collection of random integers, the amount of which is specified the user;
  11. #individual list items could be specified by replacing randint with another input block
  12. my_list = []
  13.  
  14. def bubble_sort(unsorted):
  15. #iterates through original list and copies each item into copy_list
  16. copy_list = unsorted[:]
  17.  
  18. #sets loop condition so loop will proceed
  19. swapped = True
  20. while swapped == True:
  21.  
  22. #sets loop to terminate after having iterated and swapped all necessary items in copy list
  23. swapped = False
  24. for num in range(len(copy_list)-1):
  25.  
  26. #swaps item order if a preceding number is greater than that which follows
  27. if copy_list[num] > copy_list[num + 1]:
  28. copy_list[num], copy_list[num + 1] = copy_list[num + 1], copy_list[num]
  29. #sets loop to continue until numbers are ordered
  30. swapped = True
  31. return copy_list
  32.  
  33. '''
  34. BUBBLE SORT END
  35. '''
  36.  
  37. '''
  38. MERGE SORT START
  39. '''
  40.  
  41. #Sorting algoirthm which compares two SORTED lists and merges into a single sorted list
  42. def merge(list_a, list_b):
  43. list_sorted = []
  44. while list_a != [] and list_b != []:
  45. if list_a[0] < list_b[0]:
  46. list_sorted.append(list_a.pop(0))
  47. else:
  48. list_sorted.append(list_b.pop(0))
  49. if len(list_a) < 1:
  50. list_sorted += list_b
  51. else:
  52. list_sorted += list_a
  53. return list_sorted
  54.  
  55. #Splitting alogorithm whic breaks unsorted lists in half until individual 'sorted' lists produced, which can be fed into merge function
  56. def merge_sort(unsorted):
  57. if len(unsorted) <= 1:
  58. return unsorted
  59. else:
  60. middle = len(unsorted) // 2
  61. front = unsorted[:middle]
  62. back = unsorted[middle:]
  63. front = merge_sort(unsorted[:middle])
  64. back = merge_sort(unsorted[middle:])
  65. return merge(front, back)
  66.  
  67.  
  68. '''
  69. MERGE SORT END
  70. '''
  71.  
  72.  
  73. '''
  74. TIMER PROGRAM START
  75. '''
  76.  
  77. #Generate a large (1000 items) random list
  78. list_to_sort = [randint(0,100000) for i in range(1000)]
  79.  
  80. #Create anonymous functions to use with timeit, and include Timsort algorithm for comparison
  81. bs = lambda: bubble_sort(list_to_sort)
  82. ms = lambda: merge_sort(list_to_sort)
  83. ts = lambda: sorted(list_to_sort)
  84.  
  85. #time the functions for 100 runs each
  86. print("Merge took:")
  87. print(timeit(ms, number = 100))
  88.  
  89. print("Bubble took:")
  90. print(timeit(bs, number = 100))
  91.  
  92. print("Timsort took: ")
  93. print(timeit(ts, number = 100))
  94.  
  95. '''
  96. TIMER PROGRAM END
  97. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement