def test_selection_sort(): arr = [64, 34, 25, 12, 22, 22, 11, 90] algorithm = ['bubble', 'selection', 'insert', 'merge', 'heap'] for alg in algorithm: sorting = Sorting(arr, alg) sorting.select_sorting() assert sorting._arr == [11, 12, 22, 22, 25, 34, 64, 90] assert sorting._algorithm == alg
def sorting_page(): if request.method == 'POST': import numpy as np numbers = np.random.randint(1, 1001, 500) sorting = Sorting(numbers, request.form['algorithm']) dictionary = sorting.__repr__() return render_template('sorting.html', obj=dictionary) return render_template('sorting.html')
class TestingSort(unittest.TestCase): def setUp(self): self.test_Sorting = Sorting() def test_sort_dict_values(self): self.assertEqual([1, 2, 4], self.test_Sorting.sort_dict_values({ "a": 4, "b": 2, "c": 1 }))
def binary_search(list_to_search: list[float], element: float) -> Union[int, bool]: sorted_lst: list[float] = Sorting.merge_sort(list_to_search) start: int = 0 end: int = len(sorted_lst) - 1 while start <= end: mid: int = (end + start) // 2 if sorted_lst[mid] < element: start = mid + 1 elif sorted_lst[mid] > element: end = mid - 1 else: return mid return False
def open(self, i_sel, v_sel): from sorting import Sorting env = Env() filter_list = env.get_filtered_list() sel_key, sel_val = v_sel[0], v_sel[1] def filter1(e): for key in filter_list: if key in e and e[key] < filter_list[key]: return False return True sorted_list = Sorting.perform( filter( filter1, v_sel[1] ) ) for e in sorted_list: if e["ext"] == 'G1': print("{} : width {}, height{}".format(e['filename'], e['width'], e['height'])) else: print("{}".format( e ) )
def setUp(self): self.test_Sorting = Sorting()
pemaind = Player('Messi', 10, 170, 72, ['RW', 'ST', 'CF'], 86, 93, 75, 8) pemaine = Player('Suarez', 9, 182, 86, ['ST'], 88, 86, 83, 37) pemainf = Player('Ter Stegen', 1, 187, 85, ['GK'], 66, 18, 25, 90) pemain_barca = [pemaina, pemainb, pemainc, pemaind, pemaine, pemainf] daftar_pemain = Player.daftar_pemain Tinggi = [[], []] Berat = [[], []] for players in daftar_pemain: Tinggi[1].append(players.get_height()) Tinggi[0].append(players.get_name()) Berat[1].append(players.get_weight()) Berat[0].append(players.get_name()) Sorting.quick_sort(Tinggi, 0, len(Tinggi[1]) - 1) Sorting.quick_sort(Berat, 0, len(Berat[1]) - 1) print('\nMengurutkan Berdasarkan Tinggi') for a, b in zip(Tinggi[0], Tinggi[1]): print("nama : {} \nTinggi : {}".format(a, b)) print('\nMengurutkan Berdasarkan Berat') for a, b in zip(Berat[0], Berat[1]): print("nama : {} \nBerat : {}".format(a, b)) a = Search.cari_tinggi(180) print("\nPemain dengan Tinggi diatas 180: ") for i in a: print(i)
from searching import Searching from sorting import Sorting import time sObj = Searching() sortObj = Sorting() d = { 1: "CHECK SUBTRING MATCHING", 2: "SORT NUMBERS", } def print_d(d): for k, v in d.items(): print(f"[{k}]: {v}") rep = None while rep != "exit": time.sleep(0.5) print("Choose anumber from menu : ") print_d(d) rep = int(input("> ")) if rep == 1: string = input("Enter a word: ") sub = input("Enter a substring: ") sObj.set_string_sub(string, sub) print(sObj.subString()) elif rep == 2: nums = input("Enter nums sep by white space: ").split(" ")
def insertion_sort_check(self, unsorted, expected): actual = Sorting.insertion_sort(unsorted) # sorted is a built-in function, so using actual. self.assertEqual(actual, expected)
from sorting import Sorting k = 6 arr = [4, 1, 6, 7, 8, 2, 65, 143, 5] #arr = [1] print(arr) if k > len(arr): print("Error: k larger than array") exit() sort = 'qsel' #sort = 'merge' #-----------------test mergesort implementation------------------------ if sort == 'merge': print('\nMerge-sorting...') Sorting().mergesort(arr) print(arr) print('The kth smallest element is: ', arr[k - 1]) #------------------test quickselect----------------------------------- else: print('\nQuick-selecting...') pivot = Sorting().quickSelect(arr, k) print(arr) print('The kth smallest element is: ', arr[pivot])
from sorting import Sorting array = [45, 12, 3, 5, 4, 100] sort = Sorting(array) sort.bubble_iterative() sort.bubble_modified_iterative() sort.selection_iterative() sort.insertion_iterative() sort.bucket_iterative()