def test_insert_sort_sorts_big_list(): """Test that insert sort sorts big list.""" from insert import insert_sort from random import shuffle big_list = list(range(100)) shuffle(big_list) assert insert_sort(big_list) == list(range(100))
def kruskal(arestas): arestas, vertices = insert_sort(arestas, defaultdict()) # Inicializa a árvore de fato arvore = list() # vertices terá o número de chaves do dicionário retornado pelo insertion_sort tamanhoArvore = len(vertices.keys()) i = 0 # Enquanto o tamanho da árvore é menor que o tamanho do dicionário de vértices, while len(arvore) < tamanhoArvore - 1: # Utilizamos todas as arestas aresta = arestas[i] i += 1 # Para verificar o peso das arestas com o dicionário if vertices[aresta.first] < 2 and vertices[aresta.second] < 2: vertices[aresta.first] += 1 vertices[aresta.second] += 1 arvore.append(aresta) # Não se utiliza todo o dicionário pois o tamanho da árvore quebra o while antes disso return arvore
print("bubble_sorted_list1: " + str(bubble_sorted_list1)) print("bubble_sorted_list2: " + str(bubble_sorted_list2)) print("bubble_sorted_list3: " + str(bubble_sorted_list3)) # 选择排序 list1, list2, list3 = get_list() select_sorted_list1 = select_sort(list1) select_sorted_list2 = select_sort(list2) select_sorted_list3 = select_sort(list3) print("select_sorted_list1: " + str(select_sorted_list1)) print("select_sorted_list2: " + str(select_sorted_list2)) print("select_sorted_list3: " + str(select_sorted_list3)) # 插入排序 list1, list2, list3 = get_list() insert_sorted_list1 = insert_sort(list1) insert_sorted_list2 = insert_sort(list2) insert_sorted_list3 = insert_sort(list3) print("insert_sorted_list1: " + str(insert_sorted_list1)) print("insert_sorted_list2: " + str(insert_sorted_list2)) print("insert_sorted_list3: " + str(insert_sorted_list3)) # 希尔排序 list1, list2, list3 = get_list() shell_sorted_list1 = shell_sort(list1) shell_sorted_list2 = shell_sort(list2) shell_sorted_list3 = shell_sort(list3) print("shell_sorted_list1: " + str(shell_sorted_list1)) print("shell_sorted_list2: " + str(shell_sorted_list2)) print("shell_sorted_list3: " + str(shell_sorted_list3))
def test_insert_sort_on_list_of_letters(): """Test insert sort with list of letters.""" from insert import insert_sort assert insert_sort(['b', 'f', 'p', 's', 'a']) == ['a', 'b', 'f', 'p', 's']
def test_insert_sort_on_one_item_list(): """Test insert sort with single item list.""" from insert import insert_sort assert insert_sort([5]) == [5]
def test_insert_sort_on_empty_list(): """Test insert sort returns empty list on empty list.""" from insert import insert_sort assert insert_sort([]) == []
def test_insert_sort_n_2_list(): """Test insert sort works on descending value list.""" from insert import insert_sort assert insert_sort([6, 5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5, 6]
def test_insert_sort_raises_type_error_if_input_not_list(): """Test that insert sort will raise an error if input not a list.""" with pytest.raises(TypeError): from insert import insert_sort insert_sort((1, 2, 4, 3))
def test_insert_sort_with_medium_lists(unsorted_l, sorted_l): """Test insert sort with medium lists.""" from insert import insert_sort assert insert_sort(unsorted_l) == sorted_l
def test_insert_sort_sorts_small_list(): """Test that insert sort sorts small list.""" from insert import insert_sort assert insert_sort([4, 10, 7, 1, 9]) == [1, 4, 7, 9, 10]