Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. __author__ = 'tanza'
  2.  
  3. import random, time
  4. import numpy as np
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. def mergeSortTry(part):
  12.     """takes an unsorted list and returns a sorted list
  13.    """
  14.  
  15.     try:
  16.         part[1]
  17.         mid = len(part)/2
  18.         left = mergeSortTry(part[:mid])
  19.         right = mergeSortTry(part[mid:])
  20.  
  21.         i = 0
  22.         j = 0
  23.         k = 0
  24.  
  25.         while i < len(left) and j < len(right):
  26.             if left[i] > right[j]:
  27.                 part[k] = right[j]
  28.                 j += 1
  29.             else:
  30.                 part[k] = left[i]
  31.                 i += 1
  32.             k += 1
  33.  
  34.         if i < j:
  35.             remaining = left
  36.             r = i
  37.         else:
  38.             remaining = right
  39.             r = j
  40.  
  41.         while r < len(remaining):
  42.             part[k] = remaining[r]
  43.             r += 1
  44.             k += 1
  45.  
  46.         return part
  47.     except:
  48.         return part
  49.  
  50.  
  51. def mergeSortLen(part):
  52.     """takes an unsorted list and returns a sorted list
  53.    """
  54.  
  55.     if len(part) > 1:
  56.         mid = len(part)/2
  57.         left = mergeSortLen(part[:mid])
  58.         right = mergeSortLen(part[mid:])
  59.  
  60.         i = 0
  61.         j = 0
  62.         k = 0
  63.  
  64.         llen = len(left)
  65.         rlen = len(right)
  66.         while i < llen and j < rlen:
  67.             if left[i] > right[j]:
  68.                 part[k] = right[j]
  69.                 j += 1
  70.             else:
  71.                 part[k] = left[i]
  72.                 i += 1
  73.             k += 1
  74.  
  75.         if i < j:
  76.             remaining = left
  77.             remlen = llen
  78.             r = i
  79.         else:
  80.             remaining = right
  81.             remlen = rlen
  82.             r = j
  83.  
  84.         while r < remlen:
  85.             part[k] = remaining[r]
  86.             r += 1
  87.             k += 1
  88.  
  89.         return part
  90.     else:
  91.         return part
  92.  
  93.  
  94. def timsort(arr):
  95.     arr.sort()
  96.  
  97.  
  98. def insertionSort(arr):
  99.     for i in np.arange(1, len(arr)):
  100.         currentvalue = arr[i]
  101.         position = i
  102.         while position > 0 and arr[position-1] > currentvalue:
  103.             arr[position] = arr[position-1]
  104.             position = position-1
  105.         arr[position] = currentvalue
  106.  
  107.  
  108. def quicksort(arr):
  109.     quicksorthelper(arr, 0, len(arr)-1)
  110.  
  111. def quicksorthelper(arr, first, last):
  112.     if first < last:
  113.         pivot = partition(arr, first, last)
  114.         quicksorthelper(arr, first, pivot-1)
  115.         quicksorthelper(arr, pivot+1, last)
  116.  
  117. def partition(arr, first, last) :
  118.     pivot = last
  119.     swap(arr, pivot, last)
  120.     for i in np.arange(first, last):
  121.         if arr[i] <= arr[last]:
  122.             swap(arr, i, first)
  123.             first += 1
  124.     swap(arr, first, last)
  125.     return first
  126.  
  127. def swap(A, x, y):
  128.   A[x],A[y]=A[y],A[x]
  129.  
  130.  
  131. def bubbleSort(arr):
  132.     larr = len(arr)
  133.     for i in np.arange(larr):
  134.         for j in np.arange(larr - i - 1):
  135.             if arr[j] > arr[j+1]:
  136.                 temp = arr[j+1]
  137.                 arr[j+1] = arr[j]
  138.                 arr[j] = temp
  139.  
  140. def genRandomList(n = 21, neatness = 0.50, sparsity=20):
  141.     return list(np.random.randint(low=-n*sparsity, high=n*sparsity, size=n))
  142.  
  143. def genMostlySortedList(n = 21, neatness = 0.50, sparsity=20):
  144.     arr = list(np.random.randint(low=-n*sparsity, high=n*sparsity, size=n))
  145.     arr.sort()
  146.     f = 0.05
  147.     larr = len(arr)
  148.     for i in range(int(f * larr) + 1):
  149.         x = random.randint(0, larr-1)
  150.         y = random.randint(0, larr-1)
  151.         temp = arr[x]
  152.         arr[x] = arr[y]
  153.         arr[y] = temp
  154.     return arr
  155.  
  156. def verify_correct(arr):
  157.     for item,next in zip(arr[:-1], arr[1:]):
  158.         if item <= next:
  159.             continue
  160.         else:
  161.             return False
  162.     return True
  163.  
  164.  
  165. def benchmark(method):
  166.  
  167.     for n in [10, 100]:#, 1000, 10000]:
  168.         best_runtime = None
  169.         correctness = True
  170.         for repeat in range(5):
  171.             x = genRandomList(n=n)
  172.             time_of_start = time.time()
  173.             method(x)
  174.             time_of_end = time.time()
  175.             if best_runtime is None:
  176.                 best_runtime = time_of_end - time_of_start
  177.             else:
  178.                 best_runtime = min(best_runtime, time_of_end - time_of_start)
  179.             correctness = correctness and verify_correct(x)
  180.         print "{:<20s}".format(str(method.__name__)) + ", " + "mostly sorted=False" + ", " + "{:<8s}".format(str(n)) + ", " + str("%.5f" % (best_runtime)) + ", " + str("correct" if correctness else "incorrect")
  181.  
  182.         best_runtime = None
  183.         correctness = True
  184.         for repeat in range(5):
  185.             x = genMostlySortedList(n=n)
  186.             time_of_start = time.time()
  187.             method(x)
  188.             time_of_end = time.time()
  189.             if best_runtime is None:
  190.                 best_runtime = time_of_end - time_of_start
  191.             else:
  192.                 best_runtime = min(best_runtime, time_of_end - time_of_start)
  193.             correctness = correctness and verify_correct(x)
  194.         print "{:<20s}".format(str(method.__name__)) + ", " + "mostly sorted=True " + ", " + "{:<8s}".format(str(n)) + ", " + str("%.5f" % (best_runtime)) + ", " + str("correct" if correctness else "incorrect")
  195.  
  196.     print "-----------------------------------------------------------"
  197.  
  198.  
  199. benchmark(mergeSortLen)
  200. benchmark(timsort)
  201. benchmark(quicksort)
  202. benchmark(insertionSort)
  203. benchmark(bubbleSort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement