elena_gancedo

Efficiency_Bubble_Merge-sort

Sep 5th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 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. def bubble_sort(unclassified):
  8.     changed = True
  9.     while changed == True:
  10.         changed = False
  11.         for item in range(len(unclassified) - 1):
  12.             if unclassified[item] > unclassified[item+1]:
  13.                 unclassified[item], unclassified[item+1] = unclassified[item+1], unclassified[item] # Swapping list items around
  14.                 changed = True
  15.     return unclassified
  16.  
  17. def merge(list_a, list_b):
  18.     list_c = []
  19.     while len(list_a) > 0  and len(list_b) > 0:
  20.         if list_a[0] < list_b[0]:
  21.             list_c.append(list_a.pop(0))
  22.         else:
  23.             list_c.append(list_b.pop(0))
  24.     if list_a == []:
  25.         list_c += list_b
  26.     else:
  27.         list_c += list_a
  28.     return list_c
  29.  
  30. def merge_sort(unsorted):
  31.     if len(unsorted) < 2:
  32.         return unsorted
  33.     else:
  34.         middle = len(unsorted) // 2
  35.         front = unsorted[:middle]
  36.         back = unsorted[middle:]
  37.         front = merge_sort(front)
  38.         back = merge_sort(back)
  39.     return merge(front, back)
  40.  
  41. def mergeSort(alist):
  42.    if len(alist)>1:
  43.        mid = len(alist)//2
  44.        lefthalf = alist[:mid]
  45.        righthalf = alist[mid:]
  46.        #recursion
  47.        mergeSort(lefthalf)
  48.        mergeSort(righthalf)
  49.        i=0
  50.        j=0
  51.        k=0
  52.        while i < len(lefthalf) and j < len(righthalf):
  53.            if lefthalf[i] < righthalf[j]:
  54.                alist[k]=lefthalf[i]
  55.                i=i+1
  56.            else:
  57.                alist[k]=righthalf[j]
  58.                j=j+1
  59.            k=k+1
  60.        while i < len(lefthalf):
  61.            alist[k]=lefthalf[i]
  62.            i=i+1
  63.            k=k+1
  64.  
  65.        while j < len(righthalf):
  66.            alist[k]=righthalf[j]
  67.            j=j+1
  68.            k=k+1
  69.            
  70. #Generate a large (1000 items) random list
  71. list_to_sort = [randint(0,100000) for i in range(1000)]
  72.  
  73. #Create anonymous functions to use with timeit, be sure to check these function names match your pasted ones
  74. bs = lambda: bubble_sort(list_to_sort)
  75. ms = lambda: merge_sort(list_to_sort)
  76. ps = lambda: mergeSort(list_to_sort)
  77.  
  78. #time the functions for 100 runs each
  79. print("Bubble took:")
  80. print(timeit(bs, number = 100))
  81. print("Merge took:")
  82. print(timeit(ms, number = 100))
  83. print("Other Merge took:")
  84. print(timeit(ps, number = 100))
Add Comment
Please, Sign In to add comment