def main(): a_list = [] num = int(input('Enter the number of elements: ')) for i in range(0,num): num_item = input('enter the elements in the array: ') a_list.append(num_item) bubble(a_list) print(a_list) search_element = input('enter the element you want to search for: ') bin_search(a_list , search_element)
:param function: 需要计时的函数 :return: None ''' @wraps(function) def function_timer(*args, **kwargs): print('[Function: {name} start...]'.format(name=function.__name__)) t0 = time.time() result = function(*args, **kwargs) t1 = time.time() print('[Function: {name} finished, spent time: {time:.2f}s]'.format( name=function.__name__, time=t1 - t0)) return result if __name__ == '__main__': A = np.random.randint(1, 10000, [5000]).tolist() bubble_sort.bubble(A) Insertion_Sort.insertion_sort(A) Choice_Sort.choice_sort(A) start = time.time() Merge_Sort.merge_sort(A) end = time.time() print(A) print(end - start) print() Shell_Sort.shell_sort(A) # Heap_sort.heap_sort(A) Quick_sort.quick_sort(A)
def test_bubble (self): lista=[4,1,3,6,5] ordenada = bubble (lista) self.assertEqual(ordenada, [1,3,4,5,6])
import os from bubble_sort import bubble file_path = os.getcwd() with open(os.path.join(file_path, '1.txt')) as f: file_name = os.path.basename(f.name) file1 = [file_name, f.read().split('\n')] with open(os.path.join(file_path, '2.txt')) as f: file_name = os.path.basename(f.name) file2 = [file_name, f.read().split('\n')] with open(os.path.join(file_path, '3.txt')) as f: file_name = os.path.basename(f.name) file3 = [file_name, f.read().split('\n')] files_list = bubble(file1, file2, file3) with open(os.path.join(file_path, 'End_files.txt'), 'a') as nf: for file in files_list: nf.write(file[0] + '\n') nf.write(str(len(file[1])) + '\n') nf.write('\n'.join(file[1]) + '\n')
import selection_sort import bubble_sort import insertion_sort import shell_sort print("Welcome to the program choose any one!") # In[ ]: while True: ch = int( input( 'Enter:\n[1] for Selection Sort\n[2] for Bubble Sort\n[3] for Insertion Sort\n[4] for Shell Sort\n' )) if ch == 1: selection_sort.selection() elif ch == 2: bubble_sort.bubble() elif ch == 3: insertion_sort.insertion() elif ch == 4: shell_sort.shell() else: print('Wrong Input!') t = input('Do you wanna try again ?[y/n]\t') if t == 'n' or t == 'N': print('Thank you!') break # In[ ]:
insert_arr.append(i) myfile = open("sort_methods.txt", "w") print("\nУПОРЯДОЧЕННАЯ ПОСЛЕДОВАТЕЛЬНОСТЬ: Исходный массив") print(select_arr) count = [0, 0] count = select_sort.select(select_arr, DIM) print("\nУПОРЯДОЧЕННАЯ ПОСЛЕДОВАТЕЛЬНОСТЬ: Результирующий массив") print(select_arr) CTotal[0] = count[0] MTotal[0] = count[1] count = [0, 0] count = insert_sort.insert(insert_arr, DIM) CTotal[1] = count[0] MTotal[1] = count[1] count = [0, 0] count = bubble_sort.bubble(bubble_arr, DIM) CTotal[2] = count[0] MTotal[2] = count[1] print("УПОРЯДОЧЕННАЯ ПОСЛЕДОВАТЕЛЬНОСТЬ:\n") print("Размер массива: ", DIM) print("Сравнений: ", CTotal[0], " ", CTotal[1], " ", CTotal[2]) print("Перестановок: ", MTotal[0], " ", MTotal[1], " ", MTotal[2]) select_arr.clear() bubble_arr.clear() insert_arr.clear() for i in range(DIM, 0, -1): select_arr.append(i) bubble_arr.append(i) insert_arr.append(i) print("\nОБРАТНО УПОРЯДОЧЕННАЯ ПОСЛЕДОВАТЕЛЬНОСТЬ: Исходный массив") print(select_arr)
#-*-coding:utf-8 -*- ''' 调用当前路径下所有的文件,使用其中方法 ''' # 引用本地的文件当做模块 import bubble_sort li = [123, 123, 123, 123, 12, 33, 4, 324, 3, 5, 34, 32] bubble_sort.bubble(li) # 显示bubble 函数中定义的docstring,也就是注释 print(bubble_sort.bubble.__doc__) print(bubble_sort.__doc__)