Advertisement
SavannahU

Comparing Sorting Algorithms

Mar 29th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. from timeit import timeit
  2. from random import randint
  3.  
  4. ##################################################################
  5. # COPY AND PASTE YOUR bubble_sort AND merge_sort functions below #
  6. ##################################################################
  7.  
  8. def merge(list_a, list_b):
  9.     list_c = []
  10.     while len(list_a) > 0  and len(list_b) > 0:
  11.         if list_a[0] < list_b[0]:
  12.             list_c.append(list_a.pop(0))
  13.         else:
  14.             list_c.append(list_b.pop(0))
  15.     if list_a == []:
  16.         list_c += list_b
  17.     else:
  18.         list_c += list_a
  19.     print("merged list is", list_c)
  20.     return list_c
  21.  
  22.  
  23. def merge_sort(unsorted):
  24.     if len(unsorted) < 2:
  25.         return unsorted
  26.     else:
  27.         middle = len(unsorted) // 2
  28.         front = unsorted[:middle]
  29.         back = unsorted[middle:]
  30.         print("splits are", front, back)
  31.         front = merge_sort(front)
  32.         back = merge_sort(back)
  33.     return merge(front, back)
  34.    
  35. def bubble_sort(unsorted):
  36.     #print (my_list)
  37.     # create a duplicate of the original list
  38.     my_list_duplicate = list_to_sort
  39.     # intialise a variable called swapped to true. This will be used to track if changes are made in a pass
  40.     swapped = True
  41.     # repeat the following as long as swapped is true
  42.     while swapped == True:
  43.         # set swapped to false
  44.         swapped = False
  45.         # for each item in the list
  46.         for x in range (len(my_list_duplicate)-1):
  47.             # if the current item is greater than the next item
  48.             if my_list_duplicate[x]>my_list_duplicate[x+1]:
  49.                 # set the current item of the array as current_item
  50.                 current_item = my_list_duplicate[x]
  51.                 # set the next item in the array as next_item
  52.                 next_item = my_list_duplicate[x+1]
  53.                 # change the current item to the one after it
  54.                 my_list_duplicate[x] = next_item
  55.                 # and the next position to the current one. In other words, swap them around
  56.                 my_list_duplicate[x+1] = current_item
  57.                 # set swapped to true
  58.                 swapped = True
  59.     # if no swaps were made                    
  60.     if swapped == False:    
  61.         # return the new list
  62.         return my_list_duplicate
  63.        
  64. def time_sort(unsorted):
  65.         sorted_list = sorted(unsorted)
  66.         return sorted_list
  67.  
  68.  
  69. #Generate a large (1000 items) random list
  70. list_to_sort = [randint(0,100000) for i in range(1000)]
  71.  
  72. #Create anonymous functions to use with timeit, be sure to check these function names match your pasted ones
  73. bs = lambda: bubble_sort(list_to_sort)
  74. ms = lambda: merge_sort(list_to_sort)
  75. ts = lambda: time_sort(list_to_sort)
  76.  
  77. #time the functions for 100 runs each
  78. print("Merge took:")
  79. print(timeit(ms, number = 100))
  80.  
  81. print("Bubble took:")
  82. print(timeit(bs, number = 100))
  83.  
  84. print("Timesort took:")
  85. print(timeit(ts, number = 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement