Beispiel #1
0
 def test__selection_sort(self):
     for items in self.generate_cases(self.n_cases):
         ans = sorted(items)
         t1 = time.time_ns()
         sorts.selection_sort(items)
         self.ts.append(time.time_ns() - t1)
         self.assertEqual(items, ans)
Beispiel #2
0
    def test_is_sorted(self):
        a = int_sort_list[0].copy()
        selection_sort(a)
        self.assertEqual(a, int_sort_list[1])

        b = str_sort_list[0].copy()
        selection_sort(b)
        self.assertEqual(b, str_sort_list[1])
Beispiel #3
0
 def test_selection_sort(self):
     a = [5, 8, 1, 9, 6, 3, 10, 7, 2, 4]
     b = a.copy()
     b.sort()
     selection_sort(a)
     self.assertEqual(a, b)
     c = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
     d = c.copy()
     d.sort()
     selection_sort(c)
     self.assertEqual(c, d)
Beispiel #4
0
def test_selection(array):
    print("Testing selection sort")
    start = timer()
    selection_sort(0, len(array), array)
    end = timer()
    time = end - start
    if is_sorted(array):
        print("Sort time is: ", time)
    else:
        print("Not sorted")
    return time
Beispiel #5
0
def test_selection_selection(array):
    print("Testing selection/selection combo")
    start = timer()
    selection_sort(0, len(array)//2, array)
    end = timer()
    first_half = end-start
    start = timer()
    selection_sort(0,len(array), array)
    end = timer()
    second_half = end-start
    time = first_half + second_half
    if is_sorted(array):
        print("Sort time is: ", first_half + second_half)
    else:
        print("Not sorted")
    return time
Beispiel #6
0
def data_maker_selection(liste):
    selection_sort = open("selection_sort.txt",
                          "w")  # created the text file here.
    for i in range(
            len(liste)):  # a loop to go over the slices of the original list.
        newList = liste[0:i]
        a = sorts.selection_sort(newList)
        selection_sort.write(
            str(a) + "\n")  # writing the counts to the text file line by line
    selection_sort.close()
    return print("Text file created")
Beispiel #7
0
def main():

    registers_name = read_file_by_name()
    registers_cpf = read_file_by_cpf()

    print("Dados carregados e tratados com sucesso!\n")
    print("O que deseja fazer com o vetor inicial?\n")
    print("1 - Randomizar vetor")
    print("2 - Ordem original do vetor")
    opc = input()
    if (opc == '1'):
        registers_name = random_vector(registers_name)
        registers_cpf = random_vector(registers_cpf)
    print("Qual ordenação deseja fazer?")
    print("1 - Selection Sort")
    print("2 - Insertion Sort")
    print("3 - Bubble Sort")
    opc = input()

    if (opc == '1'):
        print("Ordenar pelo nome ou pelo CPF?")
        print("1 - Nome")
        print("2 - CPF")
        opc = int(input())
        if (opc == 1):
            start = time.time()
            registers_sorted = selection_sort(registers_name)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        elif (opc == 2):
            start = time.time()
            registers_sorted = selection_sort(registers_cpf)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        else:
            print("Fim")
            return
    elif (opc == '2'):
        print("Ordenar pelo nome ou pelo CPF?")
        print("1 - Nome")
        print("2 - CPF")
        opc = int(input())
        if (opc == 1):
            start = time.time()
            registers_sorted = insertion_sort(registers_name)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        elif (opc == 2):
            start = time.time()
            registers_sorted = insertion_sort(registers_cpf)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        else:
            print("Fim")
            return
    elif (opc == '3'):
        print("Ordenar pelo nome ou pelo CPF?")
        print("1 - Nome")
        print("2 - CPF")
        opc = int(input())
        if (opc == 1):
            start = time.time()
            registers_sorted = bubble(registers_name)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        elif (opc == 2):
            start = time.time()
            registers_sorted = bubble(registers_cpf)
            end = time.time()
            elapsed_time = end - start
            sucess_menu(elapsed_time, registers_sorted)
        else:
            print("Fim")
            return
Beispiel #8
0
    i = 0
    while i != len(L):
        smallest = find_min(L, i)
        L[i], L[smallest] = L[smallest], L[i]
        i = i + 1


if __name__ == '__main__':
    import doctest
    doctest.testmod()

find_min([3, -1, 7, 5], 2)

L = [3, 4, 7, -1, 2, 5]
selection_sort(L)

# Function find_min examines each item in L[b:],
# keeping track of the minimum so far in the variable smallest.
# Whenever it finds a smaller value, it updates smallest.

# As with searching, this sorting algorithm is complex enough that
# a few examples will not be enough to test all the corner cases.
# Here is our list of test cases:
# - An empty list.
# - A list of length 1.
# - A list of length 2 (this is the shortest case in which items are moved)
# - An already-sorted list.
# - A list with all the same values.
# - A list with duplicates.
from random import randint
import logging
from copy import copy

import sorts

if __name__ == '__main__':

    N_list = [50000]
    ROUND = 1

    logging.basicConfig(level=logging.INFO)

    for N in N_list:
        randint_list = [[randint(0, 2 ** 31 - 1)
                         for _ in range(N)] for _ in range(ROUND)]

        for i in range(ROUND):
            bubble_list = copy(randint_list[i])
            insertion_list = copy(randint_list[i])
            selection_list = copy(randint_list[i])
            quick_list = copy(randint_list[i])
            heap_list = copy(randint_list[i])

            sorts.bubble_sort(bubble_list)
            sorts.insertion_sort(insertion_list)
            sorts.selection_sort(selection_list)
            sorts.quick_sort(quick_list)
            sorts.heap_sort(heap_list)
 def do_action(self):
     selection_sort(self._array)
     self._sorted = '"Selection sort" used'