Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.32 KB | None | 0 0
  1. import time
  2. import random
  3. def bubbleSort(list):
  4.     for passnum in range(len(list)-1,0,-1):
  5.         for i in range(passnum):
  6.             if list[i]>list[i+1]:
  7.                 temp = list[i]
  8.                 list[i] = list[i+1]
  9.                 list[i+1] = temp
  10. def quickSort(list):
  11.    quickSortHelper(list,0,len(list)-1)
  12.  
  13. def quickSortHelper(list,first,last):
  14.    if first<last:
  15.  
  16.        splitpoint = partition(list,first,last)
  17.  
  18.        quickSortHelper(list,first,splitpoint-1)
  19.        quickSortHelper(list,splitpoint+1,last)
  20.  
  21.  
  22. def partition(list,first,last):
  23.    pivotvalue = list[first]
  24.  
  25.    leftmark = first+1
  26.    rightmark = last
  27.  
  28.    done = False
  29.    while not done:
  30.  
  31.        while leftmark <= rightmark and list[leftmark] <= pivotvalue:
  32.            leftmark = leftmark + 1
  33.  
  34.        while list[rightmark] >= pivotvalue and rightmark >= leftmark:
  35.            rightmark = rightmark -1
  36.  
  37.        if rightmark < leftmark:
  38.            done = True
  39.        else:
  40.            temp = list[leftmark]
  41.            list[leftmark] = list[rightmark]
  42.            list[rightmark] = temp
  43.  
  44.    temp = list[first]
  45.    list[first] = list[rightmark]
  46.    list[rightmark] = temp
  47.  
  48.  
  49.    return rightmark
  50.  
  51. def mergeSort(list):
  52.     print("Splitting ",list)
  53.     if len(list)>1:
  54.         mid = len(list)//2
  55.         lefthalf = list[:mid]
  56.         righthalf = list[mid:]
  57.  
  58.         mergeSort(lefthalf)
  59.         mergeSort(righthalf)
  60.  
  61.         i=0
  62.         j=0
  63.         k=0
  64.         while i < len(lefthalf) and j < len(righthalf):
  65.             if lefthalf[i] < righthalf[j]:
  66.                 list[k]=lefthalf[i]
  67.                 i=i+1
  68.             else:
  69.                 list[k]=righthalf[j]
  70.                 j=j+1
  71.             k=k+1
  72.  
  73.         while i < len(lefthalf):
  74.             list[k]=lefthalf[i]
  75.             i=i+1
  76.             k=k+1
  77.  
  78.         while j < len(righthalf):
  79.             list[k]=righthalf[j]
  80.             j=j+1
  81.             k=k+1
  82.     print("Merging ",list)
  83. def insertionSort(list):
  84.    for index in range(1,len(list)):
  85.  
  86.      currentvalue = list[index]
  87.      position = index
  88.  
  89.      while position>0 and list[position-1]>currentvalue:
  90.          list[position]=list[position-1]
  91.          position = position-1
  92.  
  93.      list[position]=currentvalue
  94. def selectionSort(list):
  95.    for fillslot in range(len(list)-1,0,-1):
  96.        positionOfMax=0
  97.        for location in range(1,fillslot+1):
  98.            if list[location]>list[positionOfMax]:
  99.                positionOfMax = location
  100.  
  101.        temp = list[fillslot]
  102.        list[fillslot] = list[positionOfMax]
  103.        list[positionOfMax] = temp
  104. def makelist(list):
  105.     print("1: Descending order to ascending order.")
  106.     print("2: Random order to ascending order - the same random order will be used for each sorting algorithm.")
  107.     while True:
  108.         try:
  109.             x = int(input())
  110.         except:
  111.             print("ERROR: please enter an integer.")
  112.         if x == 1:
  113.             print("Please enter the amount of values in the array.")
  114.             while True:
  115.                                 try:
  116.                                         n = int(input())
  117.                                         break
  118.                                 except:
  119.                                             print("ERROR: please enter an integer.")
  120.             for i in range(0, n):
  121.                 list.append(n - 1)
  122.             break
  123.         elif x == 2:
  124.             z = False
  125.             while True:
  126.                 print("Please enter the amount of values in the array.")
  127.                 while True:
  128.                                     try:
  129.                                             n = int(input())
  130.                                             break
  131.                                     except:
  132.                                             print("ERROR: please enter an integer.")
  133.                 print("Please enter the min value.")
  134.                 while True:
  135.                                     try:
  136.                                             min = int(input())
  137.                                             break
  138.                                     except:
  139.                                             print("ERROR: please enter an integer.")
  140.                 print("Please enter the max value.")
  141.                 while True:
  142.                                     try:
  143.                                             max = int(input())
  144.                                             break
  145.                                     except:
  146.                                             print("ERROR: please enter an integer.")
  147.                 if min < max:
  148.                     break
  149.                 else:
  150.                     print("ERROR: the min value needs to be smaller than the max value")
  151.             for i in range(0, n):
  152.                 list.append(random.randint(min, max))
  153.             break
  154.         else:
  155.             print("Please enter a valid number")
  156.             x = input()
  157. def sortlist(list):
  158.     print("1: Bubble Sort")
  159.     print("2: Quick Sort")
  160.     print("3: Merge Sort")
  161.     print("4: Insertion Sort")
  162.     print("5: Selection Sort")
  163.     print()
  164.     print()
  165.     print("0: Change list")
  166.     while True:
  167.         while True:
  168.             try:
  169.                 y = int(input())
  170.                 break
  171.             except:
  172.                 print("ERROR: please enter an integer.")
  173.     if y == 0:
  174.         start = time.time()
  175.         makelist(list)
  176.         end = time.time()
  177.         print("time taken: " + int(start - end) + "s"
  178.     elif y == 1:
  179.         start = time.time()
  180.         bubblesort(list)
  181.         end = time.time()
  182.         print("time taken: " + int(start - end) + "s"
  183.     elif y == 2:
  184.         start = time.time()
  185.         quicksort(list)
  186.         end = time.time()
  187.         print("time taken: " + int(start - end) + "s"
  188.     elif y == 3:
  189.         start = time.time()
  190.         mergesort(list)
  191.         end = time.time()
  192.         print("time taken: " + int(start - end) + "s"
  193.     elif y == 4:
  194.         start = time.time()
  195.         insertionsort(list)
  196.         end = time.time()
  197.         print("time taken: " + int(start - end) + "s"
  198.     elif y == 5:
  199.         start = time.time()
  200.         selectionsort(list)
  201.         end = time.time()
  202.         print("time taken: " + int(start - end) + "s"
  203.     else:
  204.         print("Please enter a valid number.")
  205.  
  206. def main():
  207.     list = []
  208.     makelist(list)
  209.     sortlist(list)
  210.  
  211.  
  212. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement