def test_stability(): """A.""" from insertion_sort import insertion lst = [(2, 'ab'), (1, 'ba'), (3, 'ab'), (2, 'ba'), (5, 'ab')] one = lst[0] two = lst[3] sort_lst = insertion(lst) assert sort_lst == [(1, 'ba'), (2, 'ab'), (2, 'ba'), (3, 'ab'), (5, 'ab')] assert sort_lst[1] is one assert sort_lst[2] is two
def bucket(in_list): buckets = [[] for i in range(len(in_list))] # search bucket for given number for item in in_list: place = int(item * len(buckets)) buckets[place].append(item) # sorting result = [] for bucket in buckets: # skip empty bucket if len(bucket) == 0: continue # use insertion sort on full buckets if len(bucket) > 1: for item in insertion(bucket): result.append(item) # just add buckets with 1 element as it is already sorted else: result.append(bucket[0]) return result
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[ ]:
def test_insertion(): """Test with random list.""" unsorted = [randint(0, 1000000) for i in range(1000)] assert insertion(unsorted) == sorted(unsorted)
def test_insertion_in_order(): """Test doesn't mess with my list when in order.""" in_order = [i for i in range(1000)] assert insertion(in_order) == in_order
def test_insertion_empty_list(): """Test that properly handles an empty list.""" assert insertion([]) == []
def test_insertion_passed_a_string(): """Test that raises error.""" with pytest.raises(TypeError): assert insertion('hello')
def test_insertion_worse_case(): """Test with reversed list.""" reverse = [i for i in range(1000)] reverse.reverse() assert insertion(reverse) == sorted(reverse)
# how to use selection sort # give nlist as an input list to sort nlist = [9, 12, 98, 17, 22, 3] selection_sort.selectionsort(nlist) print(nlist) # how to use merge sort # give nlist as an input list to sort nlist = [23, 88, 98, 99, 12, 18, 71, 4] merge_sort.mergesort(nlist) print(nlist) # how to use insertion sort # give 'data' as an input list to sort data = [3, 8, 12, 98, 92, 64, 23, 10] insertion_sort.insertion(data) print(data) # how to use counting sort # give nlist as an input list to sort # and an integer to set it as maximum value nlist = [1, 4, 1, 2, 2, 5, 2] max_value = 7 counting_sort.counting_sort(nlist, max_value) print(nlist) # how to use bubble sort # give nlist as an input list to sort # remember that this bubble sort is sorting value from the biggest one to the lowest nlist = [71, 66, 28, 83, 90, 10] bubble_sort.bubble_sort(nlist)
def test_random_lst(): """A.""" from insertion_sort import insertion for lst in RANDOM_LIST: lst_sort = sorted(lst) assert insertion(lst) == lst_sort
def test_insertion_empty(): """Test that was can pass an empty list.""" from insertion_sort import insertion assert insertion([]) == []
def test_random_sames(): """A.""" from insertion_sort import insertion assert insertion([2, 2, 2, 1]) == [1, 2, 2, 2]