def main(): quickSortMaxedOut = False mergeSortMaxedOut = False insertionSortMaxedOut = False listSize = int(input("Initial List Size: ")) minValue = int(input("Min Item Size : ")) maxValue = int(input("Max Item Size : ")) while not quickSortMaxedOut or not mergeSortMaxedOut or not insertionSortMaxedOut: unsortedList = GenerateRandomList(listSize, minValue, maxValue) print("\n============================================================") print("List Size: ", listSize) print("------------------------------") if not insertionSortMaxedOut: start = t.time() InsertionSort(unsortedList) end = t.time() print("Insertion Sort Time: ", end - start) insertionSortMaxedOut = end - start >= 1 if not quickSortMaxedOut: start = t.time() quickSort(unsortedList) end = t.time() print("Quick Sort Time : ", end - start) quickSortMaxedOut = end - start >= 1 if not mergeSortMaxedOut: start = t.time() mergeSort(unsortedList) end = t.time() print("Merge Sort Time : ", end - start) mergeSortMaxedOut = end - start >= 1 print("============================================================") listSize = listSize * 10
def menu(): while True: win.fill((0,0,0)) draw_text('CHOSE ALGORITHM', myfont, (255, 255, 255), win, (WIDTH/2)-65, 20) mx, my = pygame.mouse.get_pos() button1 = pygame.Rect((WIDTH/2)-100, 100, 200, 50) button2 = pygame.Rect((WIDTH/2)-100, 160, 200, 50) button3 = pygame.Rect((WIDTH/2)-100, 220, 200, 50) button4 = pygame.Rect((WIDTH/2)-100, 280, 200, 50) button5 = pygame.Rect((WIDTH/2)-100, 340, 200, 50) if button1.collidepoint((mx, my)): if click: quickSort() if button2.collidepoint((mx, my)): if click: bubbleSort() if button3.collidepoint((mx, my)): if click: mergeSort() if button4.collidepoint((mx, my)): if click: insertionSort() if button5.collidepoint((mx, my)): if click: selectionSort() pygame.draw.rect(win, (255, 255, 255), button1) draw_text('QUICK SORT', myfont, (0,0,0), win, (WIDTH/2)-35, 115) pygame.draw.rect(win, (255, 255, 255), button2) draw_text('BUBBLE SORT', myfont, (0,0,0), win, (WIDTH/2)-40, 175) pygame.draw.rect(win, (255, 255, 255), button3) draw_text('MERGE SORT', myfont, (0,0,0), win, (WIDTH/2)-37, 235) pygame.draw.rect(win, (255, 255, 255), button4) draw_text('INSTERTION SORT', myfont, (0,0,0), win, (WIDTH/2)-55, 295) pygame.draw.rect(win, (255, 255, 255), button5) draw_text('SELECTION SORT', myfont, (0,0,0), win, (WIDTH/2)-53, 355) click = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pyK_ESCAPE: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True pygame.display.update() mainClock.tick(60)
def location(lst): quickSort(lst) median = 0 if len(lst) % 2 == 1: median = lst[len(lst) // 2] else: median = (lst[len(lst) // 2] + lst[len(lst) // 2 - 1]) / 2 return median
def test_quick_sort(): unsortedList = GenerateRandomList(10, 0, 30) sortedList = quickSort(unsortedList) if max(sortedList) == sortedList[-1]: print("quickSort was Correct") else: print("quickSort was Incorrect")
def sorted_growth_data(data, year1, year2): ''' this function ranks countries by absolute growth in life expectancy over a specified range of years :param data:the data structure returned by read_data function :param year1:the first year being considered :param year2:the second year being considered :return:a list of data structure Country value which is sorted in descending order ''' list = [] while data.front != None or data.back != None: o = binary_search(data.front.value.Years, year1, 0, len(data.front.value.Years) - 1) z = binary_search(data.front.value.Years, year2, 0, len(data.front.value.Years) - 1) if data.front.value.LE[o] == "" or data.front.value.LE[z] == "": dequeue(data) else: d = abs(data.front.value.LE[z] - data.front.value.LE[o]) list.append(CountryValue(data.front.value.name, d)) dequeue(data) return quickSort(list)
def sorted_ranking_data(data, year): ''' this function ranks countries by their life expectancy for a particular year :param data: the data structure returned by read_data function :param year: the year in which the countries would be ranked :return: a list of data structure Country value which is sorted in descending order ''' n = deep_copy(data) list = [] while n.front != None or n.back != None: o = binary_search(data.front.value.Years, year, 0, len(n.front.value.Years) - 1) if data.front.value.LE[o] == "": dequeue(n) else: list.append(CountryValue(n.front.value.name, n.front.value.LE[o])) dequeue(n) a = quickSort(list) print(a) print(len(a)) return a
''' Program: main.py Author: Anila Hoxha Last date modified: 06/26/2020 Implement a non recursive, in-place version of the quick-sort algorithm. ''' from quickSort import * import time # S = [7, 5, 11, 3, 2, 6] # Non-sorted list # quickSort(S) # Call the function to sort the list # print(S) # Print the sorted list S = [1] * 100000 start_time = time.time() quickSort(S) end_time = time.time() print(end_time - start_time)
def SortBoxes(boxLst): toSort = [] for box in boxLst: toSort += [[box.FreeSpace, box]] return quickSort(toSort)
def SortItems(itemLst): toSort = [] for item in itemLst: toSort += [[item.Weight, item]] return quickSort(toSort)
from quickSort import * file_name = 'QuickSort.txt' input_file = open(file_name, 'r') dataList = [] for line in input_file: data = int(line.strip()) dataList.append(data) input_file.close() # print dataList count = 0 res = quickSort(dataList) print res
#Python3 to create list of numbers n, n-1, n-2,....,2 , 1 #with n being a given from the bash script or user from quickSort import * import time import sys def backwardList(end, number): return list(range(number, end, -1)) # Driver code to test above start_time = time.time() end = 0 number = int(sys.argv[1]) arr = backwardList(end, number) print("Given array is", end="\n") print(arr) quickSort(arr, 0, len(arr) - 1) print("Sorted array is:", end="\n") print(arr) print("%s seconds" % (time.time() - start_time))