Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # sort.py
  4. #
  5. # sorts a list of items and returns the sorted list
  6. #
  7. # @author Dan
  8.  
  9. import time
  10. from time import perf_counter
  11. import random
  12.  
  13. def dansort(lst):
  14.     #print(lst)
  15.  
  16.    
  17.     if len(lst) > 1:
  18.  
  19.         mid = len(lst)//2
  20.         left = lst[:mid]
  21.         right = lst[mid:]
  22.        
  23.         dansort(left)
  24.         dansort(right)
  25.         # total = len(left) + len(right)
  26.         left_index, right_index, lst_index = 0, 0, 0
  27.        
  28.  
  29.         while left_index < len(left) and right_index < len(right)  :#
  30.                
  31.                 #print("while", left_index, right_index,len(left), len(right))
  32.                 if left[left_index] > right[right_index] :
  33.                     #print("left[i]", left[left_index], "right[i]", right[right_index])
  34.                     lst[lst_index] = right[right_index]
  35.                     #print("Rlst",lst)
  36.                     right_index += 1
  37.                 else:
  38.                     lst[lst_index] = left[left_index]
  39.                     #print("Llst",lst)
  40.                     left_index += 1
  41.                 lst_index += 1
  42.         #print(lst)
  43.         #print(left, right, left_index, right_index)
  44.         while right_index < len(right):
  45.             lst[lst_index] =  right[right_index]
  46.             #print(right_index)
  47.             right_index += 1
  48.             lst_index += 1
  49.         #print("...",left_index, len(left))
  50.         while left_index < len(left):
  51.             #print(left_index, len(left))
  52.             lst[lst_index] =  left[left_index]
  53.             #print(lst_index, left_index)
  54.             left_index += 1
  55.             lst_index += 1
  56.         #print(left, right, lst)
  57.         #print(lst)
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. ##start = time.time()
  65. ##lst = [5,4,3,2,1]
  66. ##dansort(lst)
  67. ##print(lst)
  68. ##end = time.time()
  69. ##print("{:.16f}".format(  end - start))
  70. #print(lst)
  71.  
  72. def performance_check(lst):
  73.     """ Evaluate the performance of each algorithm
  74.    """
  75.     # list of lists containing function calls with their respective names
  76.     function_calls = [['johnsort',sort1(lst)],['sort',sort(lst)],['inbuilt sort',lst.sort()],['quick sort',quick_sort(lst)],['dansort',dansort(lst)]]
  77.     avg = dict()
  78.     for function in function_calls:
  79.         avg[function[0]] = 0
  80.    
  81.     for function in function_calls:
  82.         #time.sleep(.0000001)
  83.         count_start_time        = perf_counter()
  84.         count_res               = function[1]
  85.         count_end_time          = perf_counter()
  86.         avg[function[0]] = count_end_time - count_start_time
  87.  
  88.         #print('%-15s    : {:.16f}'.format(count_end_time - count_start_time) % (function[0]))
  89.     return avg
  90.    
  91.    
  92. def random_list(n):
  93.     """returns a random list of length 'n'
  94.    """
  95.  
  96.     if n < 1:
  97.         return None
  98.     #generate a random list of length n
  99.     #then call the performance_check function on the list
  100.     rand_list = list()
  101.     previous_element = random.randint(0,9)                  #choose random first element
  102.     rand_list.append(previous_element)                      #add it to the list
  103.     for i in range(n-1):
  104.         increment = random.randint(0,3)                     #25% of time no change
  105.         previous_element = previous_element + increment
  106.         rand_list.append(previous_element)
  107.  
  108.     #print("random list:",rand_list)
  109.     return rand_list
  110.  
  111.  
  112.  
  113.  
  114. def tests():
  115.     """A suite of tests for a sorting algorithm.
  116.    OK, not a suite, just some tests.
  117.    """
  118.     sizes = [1,5,50,500,3333,50000]
  119.     #backwards
  120.     print("backwards:")
  121.    
  122.     for size in sizes:
  123.         print("\tsorting list size", size, end="\t")
  124.         lst = [i for i in range(size,0,-1)]
  125.         orig = lst[:]
  126.         #print(orig)
  127.         dansort(lst)
  128.         worked = cmp(orig, lst)
  129.         print("...",worked)
  130.    
  131.  
  132.     #random
  133.     print("random:")
  134.     for size in sizes:
  135.         print("\tsorting list size", size, end="\t")
  136.         lst = random_list(size)
  137.         orig = lst[:]
  138.         #print(orig)
  139.         dansort(lst)
  140.         worked = cmp(orig, lst)
  141.         print("...",worked)
  142.  
  143.     print("strings:")
  144.     lst = ["z","h","y","b"]
  145.     print("\tsorting", lst)
  146.     orig = lst[:]
  147.     dansort(lst)
  148.     worked = cmp(orig, lst)
  149.     print("\t\t\t\t...",worked)
  150.  
  151.     print("empty list:")
  152.     lst = []
  153.     orig = lst[:]
  154.     dansort(lst)
  155.     worked = cmp(orig, lst)
  156.     print("\t\t\t\t...",worked)
  157.    
  158.  
  159.    
  160. def cmp(lst, res):
  161.     #print(lst,"........",res)
  162.     if res == sorted(lst):
  163.         return True
  164.     return False
  165.    
  166. tests()
  167.  
  168. #stdin = input()
  169. #lst  = [int( member ) for member in input( ).split()]  
  170. #print("raw:",v_lst)
  171. #sort(lst)
  172. #print(lst)
  173. #test
  174. #lst = [4,5,3,9,10,2,5,1,7]
  175. #results = [['j--sort   ',0],['davesort  ',0],['inbuilt   ',0],['quick sort',0],['dan-sort  ',0]]
  176. #results =[0,0,0,0]
  177. ##k = 100
  178. ##for i in range(k):
  179. ##    d = perf_check_random(10)
  180. ##    results[0][1] += d['dansort']
  181. ##    results[1][1] += d['sort']
  182. ##    results[2][1] += d['inbuilt sort']
  183. ##    results[3][1] += d['quick sort']
  184. ##    results[4][1] += d['johnsort']
  185. ##    
  186. ##    
  187. ##for result in results:
  188. ##    print(result[0], 'runtime : {:.16f}'.format(result[1]/k))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement