def do_quick_sort(self): if not self.start and not self.sorted: self.start = True sorting.quickSort(self.bars, 0, len(self.bars) - 1, self) self.start = False self.sorted = True return True else: return False
def assignment2(): l = createRandomList(10) l1 = l[:] l2 = l[:] l3 = l[:] l4 = l[:] ls = sorted(l) sorting.quickSort(l1, 0, len(l1) - 1) sorting.quickSort(l2, 0, len(l2) - 1, True) sorting.mergeSort(l3) sorting.countingSort(l4) print("Unsorted: ", l) print("Epected: ", ls) print("After quick: ", l1) print("After modified quick: ", l2) print("after merge: ", l3) print("after counting: ", l4)
def main(): number_of_random_tests = 100 size_of_problems = 5 filename = "test.txt" f = lambda x,y: x>y errors = {"insertion sort":0, "merge sort":0, "quick sort":0} for _ in range(number_of_random_tests): A = random.sample(range(size_of_problems), size_of_problems) insertion_sort = sorting.insertionSort([x for x in A], f) merge_sort = sorting.mergeSort([x for x in A], f) quick_sort = [x for x in A] sorting.quickSort(quick_sort, f, 0, len(A)-1) testAlgo(A, insertion_sort, "insertion sort", errors, f) testAlgo(A, merge_sort, "merge sort", errors, f) testAlgo(A, quick_sort, "quick sort", errors, f) print("errors: \n {}".format(errors))
def main(): randomList = createRandomList(10) randomList2 = randomList[:] randomList3 = randomList[:] randomList4 = randomList[:] randomList5 = randomList[:] expectedResult = sorted(randomList[:]) print("Generated list: ", randomList) print("Expected result: ", expectedResult) sorting.bubbleSort(randomList) print("After Bubble sort: ", randomList) sorting.selectionSort(randomList2) print("After selection sort: ", randomList2) sorting.shakerSort(randomList3) print("After shaker sort: ", randomList3) sorting.mergeSort(randomList4) print("After merge sort: ", randomList4) sorting.quickSort(randomList5, 0, len(randomList5) - 1) print("After quick sort: ", randomList5)
def generar_tiempos(n): lista_insertion = [random.random() for i in range(0, n)] lista_selection = copy.deepcopy(lista_insertion) lista_bubble = copy.deepcopy(lista_insertion) lista_merge = copy.deepcopy(lista_insertion) lista_quick = copy.deepcopy(lista_insertion) tiempo_1 = time.process_time() sorting.insertionSort(lista_insertion) tiempo_2 = time.process_time() sorting.selectionSort(lista_selection) tiempo_3 = time.process_time() sorting.bubbleSort(lista_bubble) tiempo_4 = time.process_time() sorting.mergeSort(lista_merge) tiempo_5 = time.process_time() sorting.quickSort(lista_quick) tiempo_6 = time.process_time() return np.asarray( (n, tiempo_2 - tiempo_1, tiempo_3 - tiempo_2, tiempo_4 - tiempo_3, tiempo_5 - tiempo_4, tiempo_6 - tiempo_5))
def main(): if len(sys.argv) > 4: problem = loadProblem(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) else: filename = utils.getOpenFileName("problem.txt") delimeter = utils.getDelimeter(",") data_type = utils.getDataType("int") problem = loadProblem(filename, delimeter, data_type) comparisson_function = utils.getComparissonFunction("lambda x,y: x>y") print("Sample items in the problem: %s" % problem[:10]) f = eval(comparisson_function) insertion_sorted = sorting.insertionSort([x for x in problem], f) merge_sorted = sorting.mergeSort([x for x in problem], f) quick_sorted = sorting.quickSort([x for x in problem], f, 0, len(problem)-1) print("insertion sorted: %s" % insertion_sorted) print("merge sorted: %s" % merge_sorted)
if len(lis)==1: return lis[0] else: g=len(lis)//5 subM=[median.findMedian(lis[x*5:(x+1)*5]) for x in range(g)] if len(lis)%5!=0: subM.append(median.findMedian(lis[g*5:])) m=deterQuickSelect(subM,g/2 if g%2==0 else g/2+1) i,k=partitionK(lis,m) if key<=i: return deterQuickSelect(lis[:i],key) elif key<=i+len(lis)-k: return lis[k] else: return deterQuickSelect(lis[i:k],key-i-len(lis)+k) if __name__=="__main__": SIZE=100 RANGE=50 for y in range(50): test_lis=[random.randint(0,SIZE) for x in range(RANGE)] l=[] for r in range(RANGE): l.append(deterQuickSelect(test_lis,r+1)) sorting.quickSort(test_lis,0,RANGE-1) print ("RESULT",test_lis==l)
def test_1(self): arr = [random.randrange(100) for x in range(10)] result = arr.sort() self.assertEqual(result, quickSort(arr))
def test_6(self): arr = [random.randrange(100) for x in range(30)] arr2 = arr result1 = quickSort(arr) result2 = quickSort(arr2, True) self.assertEqual(result1, result2)
def test_5(self): arr = [] arr2 = arr result1 = quickSort(arr) result2 = quickSort(arr2, True) self.assertEqual(result1, result2)
from sorting import quickSort, isSorted L1 = [-5, -4, -2, 0, 1, 5, 19] L2 = reversed(L1) L3 = [-5, 10, 3, -5, 3, 2, 1, 1] # Fully sorted list assert isSorted(quickSort(L1)) and len(quickSort(L1)) == len(L1) # List that is sorted in reverse order assert isSorted(quickSort(L2)) and len(quickSort(L2)) == len(L2) # List that is not sorted assert isSorted(quickSort(L3)) and len(quickSort(L3)) == len(L3)