import time as t import sys # increases recursion limit sys.setrecursionlimit(10 ** 6) def checkSort(list): for i in range(len(list)): if list[i] != list[-1]: if list[i] > list[i + 1]: print('List IS NOT sorted correctly') return print('List IS sorted correctly') def quickSort(list, l, r): # check for single element list if l >= (r - 1): return else: piv = l i = l + 1 # partition for j in range(l + 1, r): # if j is less than pivot, swap with i if list[j] < list[piv]: list[i], list[j] = list[j], list[i] i += 1 # swap i with pivot temp = list[piv] list[piv], list[i - 1] = list[i - 1], list[piv] # first half quickSort(list, l, i - 1) # second half quickSort(list, i, r) list = [] # Test 1 # add numbers from file to list testFile = open('problem5.6test1.txt') for num in testFile: list.append(int(num)) # time and sort start = t.time_ns() quickSort(list, 0, len(list)) time = (t.time_ns() - start) / 1000000000 print('Test 1:\n' + str(list)) checkSort(list) print('Time: ' + str(time % 60) + ' seconds\n') # Test 2 list.clear() # add numbers from file to list testFile = open('problem5.6test2.txt') for num in testFile: list.append(int(num)) # time and sort start = t.time_ns() quickSort(list, 0, len(list)) time = (t.time_ns() - start) / 1000000000 print('Test 2:\n' + str(list)) checkSort(list) print('Time: ' + str(time % 60) + ' seconds\n') # Test 3 list.clear() # add numbers from file to list testFile = open('problem5.6test3.txt') for num in testFile: list.append(int(num)) # time and sort start = t.time_ns() quickSort(list, 0, len(list)) print('Test 3:\n' + str(list)) time = (t.time_ns() - start) / 1000000000 checkSort(list) print('Time: ' + str(time % 60) + ' seconds\n')