def startAlgorithm(): global data if not data: return if algMenu.get() == 'Quick Sort': quick_sort(data, 0, len(data) - 1, drawData, speedScale.get()) elif algMenu.get() == 'Bubble Sort': bubble_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Insertion Sort': insertion_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Merge Sort': merge_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Selection Sort': selection_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Selection Sort': selection_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Radix Sort': radix_sort(data, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))])
def satrtAlgo(): global data if not data: return if (algMenu.get() == 'Quick Sort'): quick_sort(data, 0, len(data) - 1, drawData, speedScale.get()) elif (algMenu.get() == 'Bubble Sort'): bubble_sort(data, drawData, speedScale.get()) elif (algMenu.get() == 'Merge Sort'): merge_sort(data, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))])
def startAlgorithm(): global data disableWidgets() if algMenu.get()=='Bubble Sort': bubbleSort(data, drawData, speedScale.get()) elif algMenu.get() == 'Merge Sort': merge_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Quick Sort': quick_sort(data, 0, len(data)-1, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))])
def startAlgorith(): global data #Calling the necessary algorith if alg_menu.get() == 'BubbleSort': bubble_sort(data, drawData, speedScale.get()) elif alg_menu.get() == 'InsertionSort': insertion_sort(data, drawData, speedScale.get()) elif alg_menu.get() == 'MergeSort': merge_sort(data, drawData, speedScale.get())
def getMsTime(percentage, data): sampleQt = math.ceil(percentage * len(data)) sample = data[:sampleQt] totalTime = 0 for x in range(3): sample2 = copy.deepcopy(sample) msStartTime = time.time() for y in range(5): ms.merge_sort(sample2) sample2 = copy.deepcopy(sample) totalTime = totalTime + (time.time() - msStartTime) return float(totalTime*1000000/3)
def setUp(self): with open("test_conversion.txt", "r") as file: file = file.read().splitlines() self.file = [int(number) for number in file] self.sorted_file = sorted(self.file) self.mergesort = merge_sort(self.file)
def runTimeMerge(aList, n, mergeTimes): start = time.time() mergeL = ms.merge_sort(aList) end = time.time() mergeTimes.append(((end - start), n)) print("{0} elements in list".format(n)) print("Merge Sort: {:.10f} seconds".format(end - start)) return mergeTimes
def startAlgorithm(): global array if not array: return # get the speed of the algorithm visualiser sizeNum = sizeEntry.get() speed = 10 / (sizeNum * pow(sizeNum, 0.5)) # visualise an algorithm if algoMenu.get() == "Insertionsort": insertion_sort(array, drawArray, speed) if algoMenu.get() == "Mergesort": merge_sort(array, drawArray, speed) if algoMenu.get() == "Bubblesort": bubble_sort(array, drawArray, speed) if algoMenu.get() == "Quicksort": quick_sort(array, drawArray, speed) if algoMenu.get() == "Heapsort": heap_sort(array, drawArray, speed)
def StartAlgorithm(): global data start = time.time() data2 = copy.deepcopy(data) if (selected_alg.get() == 'Quick Sort'): quickSort(data, 0, len(data) - 1, drawData, speedScale.get()) if (selected_alg.get() == 'Bubble Sort'): bubble_sort(data, drawData, speedScale.get()) if (selected_alg.get() == 'Merge Sort'): merge_sort(data, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))]) data = data2 messagebox.showinfo( "Data Sorted!: ", f"Sorting algorithm used: {selected_alg.get()} \nSpeed Scale: {speedScale.get()} \nTotal time taken: {time.time()-start} " )
import heapSort import random import time for i in range(1, 3): arr = [] n = 10**i for j in range(0, n): arr.append(random.randrange(0, 1000, 1)) selection_sort_time = selectionSort.selection_sort(arr, n) print("Selection sort time for size", n, "is ", selection_sort_time) arr.clear() for j in range(0, n): arr.append(random.randrange(0, 1000, 1)) start_time = time.time() arr = mergeSort.merge_sort(arr) end_time = time.time() print("The merge sorted array with size ", len(arr), "is ") for i in range(0, len(arr)): print("array[", i, "] = ", arr[i]) print("merge sort time for size", n, "is ", end_time - start_time) arr.clear() for j in range(0, n): arr.append(random.randrange(0, 1000, 1)) insertion_sort_time = insertionSort.insertion(arr) print("Insertion sort time for size", n, "is ", insertion_sort_time) arr.clear() for j in range(0, n): arr.append(random.randrange(0, 1000, 1)) start_time = time.time() array = quickSort.quicksort(arr, 0, len(arr) - 1)
from time import time import lists as test import mergeSort as sort out = open("mergesort.out", "wt") for i in test.lists: size = len(i) begin_time = time() sort.merge_sort(i) duration = time() - begin_time out.write("{} {}\n".format(size, duration)) out.close
def reduceFiles(dirPath, inputRange): starttime = time.time() asyncio.set_event_loop(asyncio.new_event_loop()) path = Path(dirPath) try: shutil.rmtree(path / 'resultsFileReducer') worked = 1 except: print('') worked = 0 while worked == 0: try: mkdir(path / 'resultsFileReducer') worked = 1 except: print('erro ao criar diretorio') print("Will read files from dir...") dirfiles = listdir(path) print(len(dirfiles)) print("Finish") print(len(dirfiles)) dirfiles.sort() picturesDict = {} # Key:pictureName Value (latitude, longitude) latLongDict = {} # Key:(lat,Long) Value pictureName namesDict = {} # Key:newSequenceName Value:oldFileName distancesDict = {} # Key:newSequenceName1-newSequenceName2 Value:distance print("Will load files") if len(dirfiles) >= 100: splits = splitArrayInNChunks(dirfiles, 100) workers = [] for splt in splits: if len(splt) > 0: t = threading.Thread(target=loadDict, args=(splt, picturesDict, latLongDict, path)) t.start() workers.append(t) for w in workers: w.join() else: for picture in dirfiles: if isfile(path / picture): loadPictureOnDict(picture, picturesDict, latLongDict, path) orderedLatLong = list(picturesDict.values()) mSort.merge_sort(orderedLatLong) print('done merge sort') f = open(path / 'resultsFileReducer' / 'orderLatLong.txt', 'w') f.write("Sequence by lat long \r\n") for latLong in orderedLatLong: f.write(str(latLong) + "\r\n") f.write('\n\rSequence fileNames:\r\n') for latLong in orderedLatLong: f.write(latLongDict[str(tuple(latLong))] + "\r\n") f.close() print("Loaded all files") print(len(picturesDict)) print(orderedLatLong) reduceConjunt(path, picturesDict, latLongDict, orderedLatLong, namesDict, distancesDict, inputRange) print("Save finished") print("time elapsed: {:.2f}s".format(time.time() - starttime)) f = open(path / 'resultsFileReducer' / 'distances.txt', 'w') f.write('\r\nDistances:\r\n') for pair in distancesDict: f.write(pair + ": " + str(distancesDict[pair]) + '\r\n') f.close() f = open(path / 'resultsFileReducer' / 'mapping.txt', 'w') f.write('\n\rnames:\r\n') for name in namesDict: f.write(name + " = " + namesDict[name] + '\r\n') f.close() return True
pattern = '(?<!\d)[a-z^\'-]+(?!\d)' ## adds all the words in our text to an array, based on the regex pattern result = re.findall(pattern, test) ##Add every "word" to the trie dictionary: dictionary = Trie() for i in range(len(result)): dictionary.add(result[i]) ##timer start: print("Starting sorting timer...") print(f"Sorting {len(result)} items.") timeStart = time.perf_counter() ## different sorting methods: merge_sort(result) ##heap_sort(result) ##Slowpokes: ##selection_sort(result) ##insertionSort(result) ##time end timeEnd = time.perf_counter() ## Sort time duration: print(f"Sorting took {timeEnd-timeStart}s") ## finds strings in array that contains only "'" or "-" forDeletion = [] for i in range(len(result)): if result[i] == "'":
bucket_sort(temp, how_many // 100) temp = array[:] comb_sort(temp) temp = array[:] counting_sort(temp) temp = array[:] heap_sort(temp) temp = array[:] insertion_sort(temp) temp = array[:] shell_sort(temp) temp = array[:] lsd_radix_sort(temp) temp = array[:] pigeonhole_sort(temp) temp = array[:] selection_sort(temp) temp = array[:] (comparisons, array_accesses, additional_space) = merge_sort(temp) print("Merge sort:") print("No. comparisons: " + str(comparisons) + ", no. array accesses: " + str(array_accesses) + ", no. additional space required: " + str(additional_space)) temp = array[:] (comparisons, array_accesses) = quick_sort(temp, 0, len(temp) - 1, 0, 0) additional_space = 0 print("Quick sort:") print("No. comparisons: " + str(comparisons) + ", no. array accesses: " + str(array_accesses) + ", no. additional space required: " + str(additional_space))
def startAlgorithm_2(): global data merge_sort(data, bar)
def gen_input(n): input_array = np.arange(n) for i in range(n): index = randint(i, n-1) swap(input_array, i, index) return input_array if __name__ == '__main__': input_dim = np.arange(500, 3000, 500) brute_force_time = [] merge_sort_time = [] for dim in input_dim: input_array = gen_input(dim) #brute force brute_force_start = time.time() brute_force_sort(input_array) brute_force_end = time.time() brute_force_time.append(brute_force_end - brute_force_start) #merge sort merge_sort_start = time.time() merge_sort(input_array, dim) merge_sort_end = time.time() merge_sort_time.append(merge_sort_end - merge_sort_start) #Visualization plt.style.use('ggplot') plt.plot(input_dim, brute_force_time, 'r--', input_dim, merge_sort_time, 'g^') plt.show()
print("Input:", test1) # SELECTION SORT TEST start = time.time() selection_sort(test1) end = time.time() border('S') space(1) print("Selection Sort Timer: ", end-start, "s", sep='') print("List Returned:", test1) space(1) border('M') # MERGE SORT TEST start = time.time() merge_sort(test2) end = time.time() border('M') space(1) print("Merge Sort Timer: ", end-start, "s", sep='') print("List Returned:", test2) space(1) border('M') # QUICK SORT TEST start = time.time() quick_sort(test3, 0, MAX - 1) end = time.time() border('M') space(1) print("Quick Sort Timer: ", end-start, "s", sep='')