def visualize_shell_sort(lst, vis): gaps = [10, 5, 3, 2, 1] start_time = time.perf_counter_ns() sort.shell_sort(lst, gaps, vis) end_time = time.perf_counter_ns() vis.replay(0) vis.reset() print('Shell Sort') print(f'Time: {end_time - start_time:,}ns\n')
def test_shell_sort(): print "\nshell 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.shell_sort(source) if tool.list_is_ascend(target) is False: print source print target print "" assert (tool.list_is_ascend(target)) print "shell sort success in {0} times.\n".format(count)
# я║тЯеепР random.shuffle(ls) time_start = time.clock() sort.select_sort(ls) 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.select_sort(ls) 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)) # ╤яеепР
def test_random_shell_sort(self): sortable = self.random_numbers sort.shell_sort(sortable)
def main(): win = gp.GraphWin('sortpy', WINDOW_X, WINDOW_Y, autoflush=False) win.setBackground(color=gp.color_rgb(43, 137, 164)) print_logo() first_input = True while 1: clear_screen(win) ds = sort.generate_dataset(sort.DATA_NUM, repeat=False) bars = [] draw_bars(ds, win, bars) sort.playback_list = [] valid_command = False if first_input: command = input('Type help to view commands\n') first_input = False else: command = input('') while not valid_command: if command == 'help': print_header('Sorting algorithms') print_subheader('Bubble sorts') print_command('bubble', 'Bubble sort') print_command('cocktail', 'Cocktail shaker sort') print_command('comb', 'Comb sort') print_command('oddeven', 'Odd-even sort') print_subheader('Insertion sorts') print_command('insertion', 'Insertion sort') print_command('shell', 'Shellsort') print_command('gnome', 'Gnome sort') print_subheader('Divide and conquer') print_command('merge', 'Merge sort') print_command('quick', 'Quicksort') print_subheader('Other') print_command('selection', 'Selection sort') print_command('stooge', 'Stooge sort') print_header('Options') print_command('quit', 'Exit sortpy') print('\n') print('Click on the window after sorting finishes to select a new algorithm') command = input('\n') elif command == 'bubble': valid_command = True elif command == 'cocktail': valid_command = True elif command == 'comb': valid_command = True elif command == 'oddeven': valid_command = True elif command == 'insertion': valid_command = True elif command == 'shell': valid_command = True elif command == 'gnome': valid_command = True elif command == 'merge': valid_command = True elif command == 'quick': valid_command = True elif command == 'selection': valid_command = True elif command == 'stooge': valid_command = True elif command == 'quit': win.close() return else: print('Command not found - type help to view commands') command = input('\n') sort.playback_list.append(ds[:]) if command == 'insertion': sort.insertion_sort(ds) elif command == 'bubble': sort.bubble_sort(ds) elif command == 'cocktail': sort.cocktail_sort(ds) elif command == 'selection': sort.selection_sort(ds) elif command == 'merge': sort.merge_sort(ds, 0, sort.DATA_NUM - 1) elif command == 'quick': sort.quick_sort(ds, 0, sort.DATA_NUM - 1) elif command == 'shell': sort.shell_sort(ds) elif command == 'gnome': sort.gnome_sort(ds) elif command == 'oddeven': sort.odd_even_sort(ds) elif command == 'comb': sort.comb_sort(ds) elif command == 'stooge': sort.stooge_sort(ds, 0, sort.DATA_NUM - 1) sort.playback_list = remove_duplicates(sort.playback_list) play_animation(ds, win, bars) win.getMouse()
import sort a = [71, 49, 69, 46, 43, 3, 73, 38, 77, 16, 65, 79, 80, 24, 2, 31, 55, 1, 82, 64] b = a[:] sort.merge_sort(b) print b b = a[:] sort.comb_sort(b) print b b = a[:] sort.shell_sort(b) print b b = a[:] sort.shell_sort(b, None, 'shell') print b b = a[:] sort.shell_sort(b, None, 'cuira') print b b = a[:] sort.insertion_sort(b) print b b = a[:] sort.coctail_sort(b) print b
def test_shell_sort(self): sortable = [1,3,5,2,4] sorted_list = [1,2,3,4,5] sort.shell_sort(sortable) self.assertEqual(sortable, sorted_list)
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("测试失败!")
if args[1] == "-in": sort.insertion_sort(data) elif args[1] == "-bu": sort.bubble_sort(data) 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))