Advertisement
pamalau

Untitled

Feb 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.93 KB | None | 0 0
  1. #------ IMPORTS ----------------------
  2. from random import randint
  3. from timeit import timeit
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. #------------------------------------
  7. #     Bubble Sort
  8. #------------------------------------
  9. def bubblesort(alist):
  10.     swapped = True
  11.     iterations = 0
  12.     list_len = len(alist)
  13.     while (swapped == True):
  14.         swapped = False
  15.         iterations += 1        
  16.         for ii in range(1,list_len):
  17.             if alist[ii-1] > alist[ii]:
  18.                 temp = alist[ii-1]
  19.                 alist[ii-1] = alist[ii]
  20.                 alist[ii] = temp
  21.                 swapped = True
  22.     return alist
  23. #-----------------------------------
  24. #  Merge Sort
  25. #-----------------------------------
  26. def merge(list_a, list_b):
  27.     list_sorted = []
  28.     while list_a != [] and list_b != []:
  29.         if list_a[0] < list_b[0]:
  30.             list_sorted.append(list_a.pop(0))
  31.         else:
  32.             list_sorted.append(list_b.pop(0))
  33.     if len(list_a) < 1:
  34.         list_sorted += list_b
  35.     else:
  36.         list_sorted += list_a
  37.     return list_sorted
  38.  
  39. def mergesort(unsorted):
  40.     if len(unsorted) <= 1:
  41.         return unsorted
  42.     else:
  43.         middle = int(len(unsorted) / 2)
  44.         front = mergesort(unsorted[:middle])
  45.         back = mergesort(unsorted[middle:])
  46.     return merge(front, back)
  47.    
  48. def dosorts(alist, blist, mlist,tlist):
  49.     alist1 = alist[:]
  50.     alist2 = alist[:]
  51.     bs = lambda: bubblesort(alist)
  52.     ms = lambda: mergesort(alist1)
  53.     ts = lambda: sorted(alist2)
  54.     #time the functions for num_iterations each
  55.     tbs = timeit(bs, number = num_iterations)
  56.     tms = timeit(ms, number = num_iterations)
  57.     tim = timeit(ts, number = num_iterations)
  58.     blist.append(tbs)    
  59.     mlist.append(tms)
  60.     tlist.append(tim)
  61.     return blist, mlist, tlist  
  62. #----------------------------------
  63. #  Testing
  64. #----------------------------------
  65. np.random.seed(555)
  66. #Generate a large (1000 items) random list
  67. max_int = 10000
  68. #num_in_list = 100
  69.  
  70. y=[]      #y Shared axes values Num Point in List
  71. xmran=[]  #x Merge sort time values Random List
  72. xbran=[]  #x Bubble sort time values Random list
  73. xtran=[]  #x Timsort sort time values Random list
  74. xmsrt=[]  #x Merge sort time values Sorted List
  75. xbsrt=[]  #x Bubble sort time values Sorted list
  76. xtsrt=[]  #x TimSort sort time values Sorted list
  77. xmrev=[]  #x Merged sort time, reversed list
  78. xbrev=[]  #x Bubble sort time, reversed list
  79. xtrev=[]  #x Timsort sort time, reversed list
  80. xmams=[]  #x Merge sort time values First and Last Items swapped
  81. xbams=[]  #x Bubble sort time values First and Last Items swapped
  82. xtams=[]  #x Timsort sort time values First and Last Items swapped
  83. num_iterations = 10
  84. #for ii in range(80):
  85. for ii in range(1, 3000,100):
  86.     print("Run : " + str(ii-1))
  87.     #num_points = ii*10 +  1
  88.     #y.append(num_points)
  89.     y.append(ii)
  90.     list_to_sort = [randint(0,max_int) for i in range(ii)]
  91.     #list_to_sort = [randint(0,max_int) for i in range(num_points)]
  92.     sortedlist = mergesort(list_to_sort)
  93.     almostsorted = sortedlist[:]
  94.     usl = len(almostsorted)
  95.     tmp = almostsorted[0];
  96.     almostsorted[0] = almostsorted[usl-1]
  97.     almostsorted[usl-1] = tmp
  98.    
  99.     xbran, xmran, xtran = dosorts(list_to_sort, xbran, xmran, xtran)   #random list
  100.     xbsrt, xmsrt, xtsrt = dosorts(sortedlist, xbsrt, xmsrt, xtsrt)     #sorted list
  101.     sortedlist.reverse()
  102.     xbrev, xmrev, xtrev = dosorts(sortedlist, xbrev, xmrev, xtrev)     #reversed list
  103.     xbams, xmams, xtams = dosorts(almostsorted, xbams, xmams, xtams)   #almost sorted list
  104. #Plot  List Data
  105. plt.style.use('seaborn-whitegrid')
  106. fig1, (ax1, ax2, ax3,ax4) = plt.subplots(1,4 , sharey=True)
  107. ax1.plot(xbran, y, "gD-", label='bubble')
  108. ax1.plot(xmran, y, 'ro-', label='merge')
  109. ax1.plot(xtran, y, 'b+-', label='timsort')
  110. ax1.legend(loc='lower right')
  111. ax1.set(xlabel='time (sec?) ' + str(num_iterations) + ' Iterations', ylabel='Length of list (number of points)',
  112.        title='Random List')
  113. ax2.plot(xbams, y, "gD-", label='bubble')
  114. ax2.plot(xmams, y, 'ro-', label='merge')
  115. ax2.plot(xtams, y, 'b+-', label='timsort')
  116. ax2.legend(loc='lower right')
  117. ax2.set(xlabel='time (sec?) ' + str(num_iterations) + ' Iterations', ylabel='Length of list (number of points)',
  118.        title='Almost Sorted List')
  119. ax3.plot(xbrev, y, "gD-", label='bubble')
  120. ax3.plot(xmrev, y, 'ro-', label='merge')
  121. ax3.plot(xtrev, y, 'b+-', label='timsort')
  122. ax3.legend(loc='lower right')
  123. ax3.set(xlabel='time (sec?) ' + str(num_iterations) + ' Iterations', ylabel='Length of list (number of points)',
  124.        title='Reversed Sorted List')  
  125. ax4.plot(xbsrt, y, "gD-", label='bubble')
  126. ax4.plot(xmsrt, y, 'ro-', label='merge')
  127. ax4.plot(xtsrt, y, 'b+-', label='timsort')
  128. ax4.legend(loc='lower right')
  129. ax4.set(xlabel='time (sec?) ' + str(num_iterations) + ' Iterations', ylabel='Length of list (number of points)',
  130.        title='Sorted List')  
  131. fig1.set_size_inches(9.5, 8.5)    
  132. fig1.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement