def visualize_heap_sort(lst, vis): start_time = time.perf_counter_ns() sort.heap_sort(lst, vis) end_time = time.perf_counter_ns() vis.replay(0) vis.reset() print('Heap Sort') print(f'Time: {end_time - start_time:,}ns\n')
def test_sort(self): # Set up array to sort array_len = 1024 max_value = 1024 x = [random.randint(0, max_value) for i in range(array_len)] # Insertion sort self.assertEqual(sort.insertion_sort(x), sorted(x)) # Merge sort self.assertEqual(sort.merge_sort(x), sorted(x)) # Selection sort self.assertEqual(sort.selection_sort(x), sorted(x)) # Bubble sort self.assertEqual(sort.bubble_sort(x), sorted(x)) # Heap sort self.assertEqual(sort.heap_sort(x), sorted(x)) # Quick sort self.assertEqual(sort.quick_sort(x), sorted(x)) # Quick sort v2 self.assertEqual(sort.quick_sort_v2(x), sorted(x))
def test_heap_sort(): print "\nheap sort begin" count = random.randint(100, 1000) for _ in range(count): length = random.randint(5, 30) source = tool.random_list(0, 100, length) target = sort.heap_sort(source) if tool.list_is_descend(target) is False: print source print target print "" assert (tool.list_is_descend(target)) print "heap sort success in {0} times.\n".format(count)
def kruskal(self, sort_method='heap'): result = [] #guarda o MST resultante i = 0 # variável de índice, usada para arestas e = 0 # variável de índice, usada para result[] if sort_method == 'heap': self.edges = heap_sort(self.edges) elif sort_method == 'counting': self.edges = counting_sort(self.edges) else: self.edges = sorted(self.edges, key=lambda item: item[2]) parent = [] rank = [] # Cria subconjuntos V com elementos únicos for node in range(len(self.graph)): parent.append(node) rank.append(0) # Número de arestas para serem tomadas é igual a V-1 while e < len(self.graph) - 1: # Step 2: Seleciona a menor aresta e incrementa # o índice para próxima iteração u, v, w = self.edges[i] i = i + 1 x = self.find(parent, u) y = self.find(parent, v) # Se, ao incluir essa aresta, não formar um ciclo, # inclui ela em resultado e incrementa o índice # do resultado para a próxima aresta if x != y: e = e + 1 result.append([u, v, w]) self.union(parent, rank, x, y) # Discarta a aresta # Printa os conteúdos de result[] pra mostrar o built MST print("Edges of MST: \n") for u, v, weight in result: #print (str(u) + " -- " + str(v) + " == " + str(weight)) print("%d -- %d == %d" % (u, v, weight)) print("\nSum of edges: ", sum(row[2] for row in result))
print ("я║тЯеепРсцй╠: % s" %(time.clock() - time_start) ) # ╡ЕхКеепР random.shuffle(ls) time_start = time.clock() sort.insert_sort(ls) print ("╡ЕхКеепРсцй╠: % s" %(time.clock() - time_start) ) # оё╤ШеепР random.shuffle(ls) time_start = time.clock() sort.shell_sort(ls) print ("оё╤ШеепРсцй╠: % s" %(time.clock() - time_start) ) # ╧И╡╒еепР random.shuffle(ls) time_start = time.clock() sort.merge_sort(ls) print ("╧И╡╒еепРсцй╠: % s" %(time.clock() - time_start) ) # ©ЛкыеепР random.shuffle(ls) time_start = time.clock() sort.quick_sort(ls) print ("©ЛкыеепРсцй╠: % s" %(time.clock() - time_start) ) # ╤яеепР random.shuffle(ls) time_start = time.clock() sort.heap_sort(ls) print ("╤яеепРсцй╠: % s" %(time.clock() - time_start) )
def test_heap_sort(self): self.assertEqual(sort.heap_sort([3, 7, 2, 1, 5, 4]), [1, 2, 3, 4, 5, 7])
def tests_heap_sort(self): """ Test Heap Sort. read sort.py for instructions. """ sample = [3, 1, 10, 654, 45, 5, 8, 5, 31, 123] sorted_sample = [1, 3, 5, 5, 8, 10, 31, 45, 123, 654] self.assertEqual(sorted_sample, sort.heap_sort(sample))
def test_heap_sort(): arr = [4, 5, 22, 6, 7, 9, 2, 3, 22, 0, 9, 1, 3, 44, 25, 4] assert sorted(arr) == heap_sort(arr)
import sort nums = [5, 7, 4, 9, 6, 3, 1] #sort.bubble_sort(nums) #sort.insert_sort(nums) #sort.select_sort(nums) #sort.merge_sort(nums, 0, len(nums)-1) #numss=sort.merge_sort1(nums) sort.heap_sort(nums) print nums
print("я║тЯеепРсцй╠: % s" % (time.clock() - time_start)) # ╡ЕхКеепР random.shuffle(ls) time_start = time.clock() sort.insert_sort(ls) print("╡ЕхКеепРсцй╠: % s" % (time.clock() - time_start)) # оё╤ШеепР random.shuffle(ls) time_start = time.clock() sort.shell_sort(ls) print("оё╤ШеепРсцй╠: % s" % (time.clock() - time_start)) # ╧И╡╒еепР random.shuffle(ls) time_start = time.clock() sort.merge_sort(ls) print("╧И╡╒еепРсцй╠: % s" % (time.clock() - time_start)) # ©ЛкыеепР random.shuffle(ls) time_start = time.clock() sort.quick_sort(ls) print("©ЛкыеепРсцй╠: % s" % (time.clock() - time_start)) # ╤яеепР random.shuffle(ls) time_start = time.clock() sort.heap_sort(ls) print("╤яеепРсцй╠: % s" % (time.clock() - time_start))
#再帰条件の変更 import sys sys.setrecursionlimit(100000) l = list(range(3000)) random.shuffle(l) start_time = time.time() sorted_list = sort.bubble_sort(l) print('time:{0:.3f}'.format(time.time()-start_time)) start_time = time.time() sorted_list = sort.selection_sort(l) print('time:{0:.3f}'.format(time.time()-start_time)) start_time = time.time() sorted_list = sort.insertion_sort(l) print('time:{0:.3f}'.format(time.time()-start_time)) start_time = time.time() sorted_list = sort.heap_sort(l) print('time:{0:.3f}'.format(time.time()-start_time)) start_time = time.time() sorted_list = sort.merge_sort(l) #print(sorted_list) print('time:{0:.3f}'.format(time.time()-start_time)) start_time = time.time() sorted_list = sort.quick_sort(l) print('time:{0:.3f}'.format(time.time()-start_time))
print '' print 'insertion sort\n' list = sort.insert_sort(list_large[:]) assert sort.check_sorted(list) timer.get_time(time.clock()) print '' print 'insertion sort fast\n' list = sort.insert_sort_fast(list_large[:]) assert sort.check_sorted(list) timer.get_time(time.clock()) print '' print 'heap sort\n' list = sort.heap_sort(list_large[:]) assert sort.check_sorted(list) timer.get_time(time.clock()) print '' list_large = [] for i in range(50000): list_large.append(random.randint(1, 100)) print 'size:50000, for the faster ones' timer.get_time(time.clock(), False) #update time print 'merge sort\n' list = sort.merge_sort(list_large[:]) assert sort.check_sorted(list) timer.get_time(time.clock())
def test(n): data=generate_data(n) datas=[copy.deepcopy(data) for _ in range(7)] correct=generate_data(20) corrects=[copy.deepcopy(correct) for _ in range(7)] print("正确检测:") print("原始数据:",correct) sort.bubble_sort(corrects[0]) sort.insertion_sort(corrects[1]) sort.selection_sort(corrects[2]) sort.quick_sort(corrects[3],0,19) result=sort.merge_sort(corrects[4]) sort.shell_sort(corrects[5],2) sort.heap_sort(corrects[6]) print("冒泡排序:",corrects[0]) print("插入排序:",corrects[1]) print("选择排序:",corrects[2]) print("快速排序:",corrects[3]) print("归并排序:",result) print("希尔排序:",corrects[5]) print("堆排序:",corrects[6]) print() print("计算时间检测:") start=time.clock() sort.bubble_sort(datas[0]) end=time.clock() print("冒泡排序运行时间:",end-start) start=time.clock() sort.insertion_sort(datas[1]) end=time.clock() print("插入排序运行时间:",end-start) start=time.clock() sort.selection_sort(datas[2]) end=time.clock() print("选择排序运行时间:",end-start) start=time.clock() sort.quick_sort(datas[3],0,len(datas[3])-1) end=time.clock() print("快速排序运行时间:",end-start) start=time.clock() datas[4]=sort.merge_sort(datas[4]) end=time.clock() print("归并排序运行时间:",end-start) start=time.clock() sort.shell_sort(datas[5],2) end=time.clock() print("希尔排序运行时间:",end-start) start=time.clock() sort.heap_sort(datas[6]) end=time.clock() print("堆排序运行时间:",end-start) if datas[0]==datas[1]==datas[2]==datas[3]==datas[4]==datas[5]==datas[6]: print("测试通过!") else: print("测试失败!")
elif args[1] == "-se": sort.selection_sort(data) elif args[1] == "-me": sort.merge_sort(data) elif args[1] == "-qu": sort.quick_sort(data) elif args[1] == "-lsd": sort.lsd_radix_sort(data) elif args[1] == "-msd": sort.msd_radix_sort(data) elif args[1] == "-sh": sort.shell_sort(data) elif args[1] == "-sk": sort.shaker_sort(data) elif args[1] == "-he": sort.heap_sort(data) elif args[1] == "-cm": sort.comb_sort(data) elif args[1] == "-oe": sort.oddeven_sort(data) elif args[1] == "-gn": sort.gnome_sort(data) elif args[1] == "-gr": sort.gravity_sort(data) elif args[1] == "-st": print("[log] reset array") data = array.array('i', range(1, 30)) mix_data(data) sort.stooge_sort(data) else: sys.exit("[error] select option")