def testSelection(self):
     result = copy(self.original)
     before = time.time()
     selection_sort(result)
     after = time.time()
     print("Selection Sort, size: %d time: %f" % (self.list_length, after-before))
     self.assertEqual(self.sorted_list, result, "Selection Sort Failed")
Esempio n. 2
0
def iterations(sort_type, n):  # This function sorts randomly generated numbers of length n that range from -500 to 500
	if sort_type == "selection sort":
		selection_sort.selection_sort([random.randint(-500,500) for number in range(n)])
	elif sort_type == "merge sort":
		merge_sort.merge_sort([random.randint(-500,500) for number in range(n)])
	elif sort_type == "quick sort":
		quick_sort.quick_sort([random.randint(-500,500) for number in range(n)])
Esempio n. 3
0
 def test_sort_array(self):
     array1 = [5, 65, -5, 12, 56, 80, 8, 12]
     sort.selection_sort(array1)
     self.assertEqual([-5, 5, 8, 12, 12, 56, 65, 80], array1)
     array2 = [5]
     sort.selection_sort(array2)
     self.assertEqual([5], array2)
Esempio n. 4
0
def test_selection():
    sorted_list = [i for i in range(5000)]

    unsorted = sorted_list.copy()
    random.shuffle(unsorted)

    selection_sort(unsorted)

    assert_list_equal(sorted_list, unsorted)
    def test_selection_sort(self):
        arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
        arr2 = []
        arr3 = [0, 1, 2, 3, 4, 5]
        arr4 = random.sample(range(200), 50)

        self.assertEqual(selection_sort(arr1), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(selection_sort(arr2), [])
        self.assertEqual(selection_sort(arr3), [0, 1, 2, 3, 4, 5])
        self.assertEqual(selection_sort(arr4), sorted(arr4))
Esempio n. 6
0
def quick_sort(a, start_index, end_index):
    if(start_index >= end_index):
        return 
    length = end_index - start_index + 1
    if(length<20):
        selection_sort.selection_sort(a,start_index,length)
        return
    randomize_input(a, start_index, end_index)
    split = partition(a, start_index, end_index)
    quick_sort(a, start_index, split - 1)
    quick_sort(a, split + 1, end_index) 
Esempio n. 7
0
def quick_sort(a, start_index, end_index):
    if (start_index >= end_index):
        return
    length = end_index - start_index + 1
    if (length < 20):
        selection_sort.selection_sort(a, start_index, length)
        return
    randomize_input(a, start_index, end_index)
    split = partition(a, start_index, end_index)
    quick_sort(a, start_index, split - 1)
    quick_sort(a, split + 1, end_index)
Esempio n. 8
0
    def test_selection_sort(self):
        lst1 = [1, 2, 3, 4, 5]
        lst1 = selection_sort(lst1)
        self.assertEqual(lst1, [1, 2, 3, 4, 5])

        lst2 = [5, 4, 3, 2, 1]
        lst2 = selection_sort(lst2)
        self.assertEqual(lst2, [1, 2, 3, 4, 5])

        lst3 = [2, 4, 3, 4, 5, 5, 1]
        lst3 = selection_sort(lst3)
        self.assertEqual(lst3, [1, 2, 3, 4, 4, 5, 5])
Esempio n. 9
0
    def test_selection_sort(self):
        arr = [3, 1, 4, 2, 5, 0]
        result = selection_sort(arr)
        self.assertEqual(result, [0, 1, 2, 3, 4, 5])

        arr = [1, 3, 2, 0, 6, 9, 8]
        result = selection_sort(arr)
        self.assertEqual(result, [0, 1, 2, 3, 6, 8, 9])

        arr = [1, -3, -2, 6, 6, 9, 6]
        result = selection_sort(arr)
        self.assertEqual(result, [-3, -2, 1, 6, 6, 6, 9])
def test_selection_sort():
    for i in range(500):
        array = []

        for j in range(i):
            array.append(random.randrange(-i, i, 1))

        temp = array.copy()
        temp.sort()
        selection_sort(array)

        assert temp == array
Esempio n. 11
0
    def test_selection_sort(self):
        lst1 = [1, 2, 3, 4, 5]
        lst1 = selection_sort(lst1)
        self.assertEqual(lst1, [1, 2, 3, 4, 5])

        lst2 = [5, 4, 3, 2, 1]
        lst2 = selection_sort(lst2)
        self.assertEqual(lst2, [1, 2, 3, 4, 5])

        lst3 = [2, 4, 3, 4, 5, 5, 1]
        lst3 = selection_sort(lst3)
        self.assertEqual(lst3, [1, 2, 3, 4, 4, 5, 5])
Esempio n. 12
0
def run_selection_sort():
    rand_arr = utils.generate_random_array(100)
    copy = list(rand_arr)

    rand_arr.sort()
    approx_iterations = selection_sort(copy)
    assert_true(utils.compare_arrays(rand_arr, copy), True, 'Selection Sort')
Esempio n. 13
0
def main():
    """Função principal que será rodada quando o script for passado para o interpretador."""

    # A função abaixo abre o arquivo texto numeros.txt em modo leitura e lê as linhas dele separando
    # elas em uma lista de strings.
    with open('números.txt', 'r', encoding='utf8') as arquivo:
        linhas_do_arquivo = arquivo.readlines()

    # Pegue a lista de strings e converta todos os valores dentro dela para inteiros.
    # TODO COLOQUE SEU CÓDIGO AQUI E APAGUE ESSE COMENTÁRIO DEPOIS.
    for lis in linhas_do_arquivo:
        linhas_do_arquivo.append(int(lis))

    # O Código abaixo chama cada um dos métodos de ordenação na lista original.
    # Para garantir que a lista original não muda depois de cada uma das chamadas.
    # Fazemos cópias dela antes de fazer a chamada.
    # A função process_time serve para marcar o tempo entre uma e outra chamada das funções e vermos
    # qual das três é mais rápida.
    lista_copiada = linhas_do_arquivo.copy()
    tempo_bubble = time.process_time()
    lista_ordenada_bubble = bubble_sort(lista_copiada)
    tempo_bubble = time.process_time() - tempo_bubble
    lista_copiada = linhas_do_arquivo.copy()
    tempo_merge = time.process_time()
    lista_ordenada_merge = merge_sort(lista_copiada)
    tempo_merge = time.process_time() - tempo_merge
    lista_copiada = linhas_do_arquivo.copy()
    tempo_selection = time.process_time()
    lista_ordenada_selection = selection_sort(lista_copiada)
    tempo_selection = time.process_time() - tempo_selection
    print('Tempo de demora do Bubble Sort:', tempo_bubble)
    print('Tempo de demora do Merge Sort:', tempo_merge)
    print('Tempo de demora do Selection Sort:', tempo_selection)
    return lista_ordenada_bubble, lista_ordenada_merge, lista_ordenada_selection
Esempio n. 14
0
def run_selection_sort():
    rand_arr = utils.generate_random_array(100)
    copy = list(rand_arr)

    rand_arr.sort()
    approx_iterations = selection_sort(copy)
    assert_true(utils.compare_arrays(rand_arr, copy), True, 'Selection Sort')
Esempio n. 15
0
def time_selection_search():
    elapsed = 0
    size = 0
    size_inc = 10000
    print('')

    while elapsed < 60:
        size += size_inc
        ints = [random.randrange(0, size) for x in range(size)]

        start = time.perf_counter()
        selection_sort.selection_sort(ints)
        end = time.perf_counter()
        elapsed = end - start

        print(size, '\t{0:.5f}'.format(elapsed, 'sec.\n'))
Esempio n. 16
0
def main():
    sample = list(np.random.randint(1000, size=2000))
    target = sorted(sample)

    start = timer()
    bubble = bubble_sort(sample.copy())
    end = timer()
    assert bubble == target
    print("bubble sort time (ms) %.4f" % ((end - start) * 1000))

    start = timer()
    selection = selection_sort(sample.copy())
    end = timer()
    assert selection == target
    print("selection sort time (ms) %.4f" % ((end - start) * 1000))

    start = timer()
    merge = merge_sort(sample.copy())
    end = timer()
    assert merge == target
    print("merge sort time (ms) %.4f" % ((end - start) * 1000))

    start = timer()
    quick = quick_sort(sample.copy(), 0, len(sample) - 1)
    end = timer()
    assert quick == target
    print("quick sort time (ms) %.4f" % ((end - start) * 1000))
Esempio n. 17
0
def benchmark(n = [10, 100, 1000, 5000, 10000]):
    """ Benchmark the 6 sorting algorithms """

    times = {'bubble':[], 'selection':[], 'merge':[], 'quicksort3':[], 'insertion_swap':[], 'insertion_ass':[]}
    
    for size in n:
        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        bubble_sort(a)
        t1 = clock()
        times['bubble'].append(t1-t0)
        
        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        selection_sort(a)
        t1 = clock()
        times['selection'].append(t1-t0)

        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        merge_sort(a)
        t1 = clock()
        times['merge'].append(t1-t0)

        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        insertion_sort_swap(a)
        t1 = clock()
        times['insertion_swap'].append(t1-t0)

        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        insertion_sort_assignment(a)
        t1 = clock()
        times['insertion_ass'].append(t1-t0)

        a = create_array(size = size, max_num = 10*size)
        t0 = clock()
        quicksort3(a, 0, size)
        t1 = clock()
        times['quicksort3'].append(t1-t0)

    print(98*'_')
    print("n\tBubble\t   Insertion(s)\t\tInsertion(a)\t   Merge\tQuicksort3\tSelection")
    print(98*'_')
    for i, size in enumerate(n):
        print("%d\t%5.4f\t     %5.4f\t\t  %5.4f\t  %5.4f\t  %5.4f\t %5.4f"%(size, times['bubble'][i], times['insertion_swap'][i], times['insertion_ass'][i], times['merge'][i], times['quicksort3'][i], times['selection'][i]))
Esempio n. 18
0
def main():
    # Search for a number
    array_length, array, min_val, max_val = set_constants()
    x = generate_int_list(array_length, array, min_val, max_val)
    x = selection_sort(array_length, x)
    #index = sequential_sort_and_search(array = x, key = 147)
    index = sequential_search(array = x, key = 147)
    print(verify_search(array = x, index = index, key = 147))
Esempio n. 19
0
 def test_selection_sort(self):
     """
     Test that it can sort a list of numbers with the selection algorithm.
     """
     data = [4, 78, 2, 33, 0, 99, 86, 4, 21, 3]
     expected = [0, 2, 3, 4, 4, 21, 33, 78, 86, 99]
     output = selection_sort(data)
     self.assertEqual(output, expected)
 def test_case_12(self):
     self.assertEqual(
         program.selection_sort([
             4, 1, 5, 0, -9, -3, -3, 9, 3, -4, -9, 8, 1, -3, -7, -4, -9, -1,
             -7, -2, -7, 4
         ]), [
             -9, -9, -9, -7, -7, -7, -4, -4, -3, -3, -3, -2, -1, 0, 1, 1, 3,
             4, 4, 5, 8, 9
         ])
 def test_case_11(self):
     self.assertEqual(
         program.selection_sort([
             2, -2, -6, -10, 10, 4, -8, -1, -8, -4, 7, -4, 0, 9, -9, 0, -9,
             -9, 8, 1, -4, 4, 8, 5, 1, 5, 0, 0, 2, -10
         ]), [
             -10, -10, -9, -9, -9, -8, -8, -6, -4, -4, -4, -2, -1, 0, 0, 0,
             0, 1, 1, 2, 2, 4, 4, 5, 5, 7, 8, 8, 9, 10
         ])
 def test_case_9(self):
     self.assertEqual(
         program.selection_sort([
             8, -6, 7, 10, 8, -1, 6, 2, 4, -5, 1, 10, 8, -10, -9, -10, 8, 9,
             -2, 7, -2, 4
         ]), [
             -10, -10, -9, -6, -5, -2, -2, -1, 1, 2, 4, 4, 6, 7, 7, 8, 8, 8,
             8, 9, 10, 10
         ])
 def test_case_7(self):
     self.assertEqual(
         program.selection_sort([
             -4, 5, 10, 8, -10, -6, -4, -2, -5, 3, 5, -4, -5, -1, 1, 6, -7,
             -6, -7, 8
         ]), [
             -10, -7, -7, -6, -6, -5, -5, -4, -4, -4, -2, -1, 1, 3, 5, 5, 6,
             8, 8, 10
         ])
def start_algo():
    global data
    if not data:
        return

    if algmenu.get() == 'Bubble Sort':
        bubble_sort(data, drawdata, speed_scale.get())
    elif algmenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, drawdata, speed_scale.get())
    elif algmenu.get() == 'Insertion Sort':
        insert_sort(data, drawdata, speed_scale.get())
    elif algmenu.get() == 'Selection Sort':
        selection_sort(data, drawdata, speed_scale.get())
    elif algmenu.get() == 'Merge Sort':
        merge_sort(data, drawdata, speed_scale.get())
    for i in range(len(data)):
        drawdata(data, [
            'salmon' if x == i else 'deep sky blue' for x in range(len(data))
        ])
    drawdata(data, ['spring green' for x in range(len(data))])
Esempio n. 25
0
def find_winner():
    array = []
    for _ in range(n):
        array.append(randint(0, n))

    temp_list = array.copy()
    bubble_start_time = time.time()
    bubble_sort(temp_list, n)
    bubble_end_time = time.time()
    bubble_complete_time = bubble_end_time - bubble_start_time

    temp_list = array.copy()
    selection_start_time = time.time()
    selection_sort(temp_list, n)
    selection_end_time = time.time()
    selection_complete_time = selection_end_time - selection_start_time

    temp_list = array.copy()
    insertion_start_time = time.time()
    insertion_sort(temp_list, n)
    insertion_end_time = time.time()
    insertion_complete_time = insertion_end_time - insertion_start_time

    temp_list = array.copy()
    python_sort_start_time = time.time()
    sorted(temp_list)
    python_sort_end_time = time.time()
    python_sort_complete_time = python_sort_end_time - python_sort_start_time

    print(bubble_complete_time)
    print(selection_complete_time)
    print(insertion_complete_time)
    print(python_sort_complete_time)
    print('One cycle ended')

    time_arr = [
        bubble_complete_time, selection_complete_time, insertion_complete_time,
        python_sort_complete_time
    ]
    return find_min_index(time_arr)
 def test_case_14(self):
     self.assertEqual(
         program.selection_sort([
             991, -731, -882, 100, 280, -43, 432, 771, -581, 180, -382,
             -998, 847, 80, -220, 680, 769, -75, -817, 366, 956, 749, 471,
             228, -435, -269, 652, -331, -387, -657, -255, 382, -216, -6,
             -163, -681, 980, 913, -169, 972, -523, 354, 747, 805, 382,
             -827, -796, 372, 753, 519, 906
         ]), [
             -998, -882, -827, -817, -796, -731, -681, -657, -581, -523,
             -435, -387, -382, -331, -269, -255, -220, -216, -169, -163,
             -75, -43, -6, 80, 100, 180, 228, 280, 354, 366, 372, 382, 382,
             432, 471, 519, 652, 680, 747, 749, 753, 769, 771, 805, 847,
             906, 913, 956, 972, 980, 991
         ])
 def test_case_13(self):
     self.assertEqual(
         program.selection_sort([
             427, 787, 222, 996, -359, -614, 246, 230, 107, -706, 568, 9,
             -246, 12, -764, -212, -484, 603, 934, -848, -646, -991, 661,
             -32, -348, -474, -439, -56, 507, 736, 635, -171, -215, 564,
             -710, 710, 565, 892, 970, -755, 55, 821, -3, -153, 240, -160,
             -610, -583, -27, 131
         ]), [
             -991, -848, -764, -755, -710, -706, -646, -614, -610, -583,
             -484, -474, -439, -359, -348, -246, -215, -212, -171, -160,
             -153, -56, -32, -27, -3, 9, 12, 55, 107, 131, 222, 230, 240,
             246, 427, 507, 564, 565, 568, 603, 635, 661, 710, 736, 787,
             821, 892, 934, 970, 996
         ])
Esempio n. 28
0
    def test_when_arr_is_not_correct_type(self):

        with self.assertRaises(TypeError):
            s.selection_sort("hello", 2)

        with self.assertRaises(TypeError):
            s.selection_sort([1, 2, 3], "hi")

        with self.assertRaises(TypeError):
            s.selection_sort("hello", "hi")
Esempio n. 29
0
def main():
  if len(sys.argv) != 2:
    print 'usage: ./compare_sort_algos.py --len_of_array'
    sys.exit(1)

  len_of_array = sys.argv[1] # This argument has length of the array to be sorted.
  print len_of_array
  # Create Random numbers of this length. The random numbers generated are unique.
  array = random.sample(xrange(10000000), int(len_of_array))
  #print array
  sorted_array = insertion_sort.insertion_sort(array)
  insertion_time = time.clock()
  insertion_tot = insertion_time - start_time
  print ("Insertion Sort %s" % insertion_tot)
  sorted_array = selection_sort.selection_sort(array)
  selection_time = time.clock()
  selection_tot = selection_time - insertion_time
  print ("Selection Sort %s" % (selection_tot))
  sorted_array = bubble_sort.bubble_sort(array)
  bubble_time = time.clock()
  bubble_tot = bubble_time - selection_time
  print ("Bubble Sort %s" % (bubble_tot))
  sorted_array_m = merge_sort.merge_sort(array)
  merge_time = time.clock()
  merge_tot = merge_time - bubble_time
  print ("Merge Sort %s" % (merge_tot))
  sorted_array_q = quick_sort.quick_sort(array)
  quick_time = time.clock()
  quick_tot = quick_time - merge_time
  print ("Quick Sort %s" % (quick_tot))
  sorted_array_h = heap_sort.heap_sort(array)
  heap_time = time.clock()
  heap_tot = heap_time - quick_time
  print ("Heap Sort %s" % (heap_tot))
  
  objects = ('Insertion', 'Selection', 'Bubble', 'Merge','Quick','Heap')
  y_pos = np.arange(len(objects))
  performance = [insertion_tot/merge_tot,selection_tot/merge_tot,bubble_tot/merge_tot,merge_tot/merge_tot,quick_tot/merge_tot,heap_tot/merge_tot]
 
  if (sorted_array_m == sorted_array_q):
	print "Merge and Quick sorts are giving the same sorted array results"
  plt.bar(y_pos, performance, align='center', alpha=0.5)
  plt.xticks(y_pos, objects)
  plt.ylabel('Time taken w.r.t merge sort')
  plt.title('Sorting Techniques')
 
  plt.show()
 def test_case_16(self):
     self.assertEqual(
         program.selection_sort([
             544, -578, 556, 713, -655, -359, -810, -731, 194, -531, -685,
             689, -279, -738, 886, -54, -320, -500, 738, 445, -401, 993,
             -753, 329, -396, -924, -975, 376, 748, -356, 972, 459, 399,
             669, -488, 568, -702, 551, 763, -90, -249, -45, 452, -917, 394,
             195, -877, 153, 153, 788, 844, 867, 266, -739, 904, -154, -947,
             464, 343, -312, 150, -656, 528, 61, 94, -581
         ]), [
             -975, -947, -924, -917, -877, -810, -753, -739, -738, -731,
             -702, -685, -656, -655, -581, -578, -531, -500, -488, -401,
             -396, -359, -356, -320, -312, -279, -249, -154, -90, -54, -45,
             61, 94, 150, 153, 153, 194, 195, 266, 329, 343, 376, 394, 399,
             445, 452, 459, 464, 528, 544, 551, 556, 568, 669, 689, 713,
             738, 748, 763, 788, 844, 867, 886, 904, 972, 993
         ])
Esempio n. 31
0
def main():
    algorithms = {
        'insertion_sort': lambda a: insertion_sort(a),
        'quick_sort': lambda a: quick_sort(a),
        'selection_sort': lambda a: selection_sort(a),
        'np_quicksort': lambda a: np.sort(a, kind='quicksort'),
        'np_mergesort': lambda a: np.sort(a, kind='mergesort')
    }

    sizes = list(range(1, 100, 5)) + list(range(200, 5000, 50))
    avg_time = {alg: [] for alg in algorithms}
    for sz in tqdm(sizes):
        for alg_name, f in algorithms.items():
            avg_time[alg_name].append(measure_search_time(f, sz, 100))

    for alg_name in algorithms:
        plt.plot(sizes, avg_time[alg_name], label=alg_name)
    plt.legend()
    plt.show()
Esempio n. 32
0
def test_selection_sort():
    # get data from file
    print("test selection sort.")
    in_data = []
    for line in fileinput.input("../../data/sort1.dat"):
        in_data.append(int(line))
    print("in_data", in_data)

    target_data = in_data.copy()

    target_data.sort()
    print("target_data", target_data)

    from selection_sort import selection_sort

    out_data = selection_sort(in_data)
    print("out_data", out_data)

    assert (out_data == target_data)
 def test_case_15(self):
     self.assertEqual(
         program.selection_sort([
             384, -67, 120, 759, 697, 232, -7, -557, -772, -987, 687, 397,
             -763, -86, -491, 947, 921, 421, 825, -679, 946, -562, -626,
             -898, 204, 776, -343, 393, 51, -796, -425, 31, 165, 975, -720,
             878, -785, -367, -609, 662, -79, -112, -313, -94, 187, 260, 43,
             85, -746, 612, 67, -389, 508, 777, 624, 993, -581, 34, 444,
             -544, 243, -995, 432, -755, -978, 515, -68, -559, 489, 732,
             -19, -489, 737, 924
         ]), [
             -995, -987, -978, -898, -796, -785, -772, -763, -755, -746,
             -720, -679, -626, -609, -581, -562, -559, -557, -544, -491,
             -489, -425, -389, -367, -343, -313, -112, -94, -86, -79, -68,
             -67, -19, -7, 31, 34, 43, 51, 67, 85, 120, 165, 187, 204, 232,
             243, 260, 384, 393, 397, 421, 432, 444, 489, 508, 515, 612,
             624, 662, 687, 697, 732, 737, 759, 776, 777, 825, 878, 921,
             924, 946, 947, 975, 993
         ])
 def test_case_18(self):
     self.assertEqual(
         program.selection_sort([
             -823, 164, 48, -987, 323, 399, -293, 183, -908, -376, 14, 980,
             965, 842, 422, 829, 59, 724, -415, -733, 356, -855, -155, 52,
             328, -544, -371, -160, -942, -51, 700, -363, -353, -359, 238,
             892, -730, -575, 892, 490, 490, 995, 572, 888, -935, 919, -191,
             646, -120, 125, -817, 341, -575, 372, -874, 243, 610, -36,
             -685, -337, -13, 295, 800, -950, -949, -257, 631, -542, 201,
             -796, 157, 950, 540, -846, -265, 746, 355, -578, -441, -254,
             -941, -738, -469, -167, -420, -126, -410, 59
         ]), [
             -987, -950, -949, -942, -941, -935, -908, -874, -855, -846,
             -823, -817, -796, -738, -733, -730, -685, -578, -575, -575,
             -544, -542, -469, -441, -420, -415, -410, -376, -371, -363,
             -359, -353, -337, -293, -265, -257, -254, -191, -167, -160,
             -155, -126, -120, -51, -36, -13, 14, 48, 52, 59, 59, 125, 157,
             164, 183, 201, 238, 243, 295, 323, 328, 341, 355, 356, 372,
             399, 422, 490, 490, 540, 572, 610, 631, 646, 700, 724, 746,
             800, 829, 842, 888, 892, 892, 919, 950, 965, 980, 995
         ])
 def test_case_17(self):
     self.assertEqual(
         program.selection_sort([
             -19, 759, 168, 306, 270, -602, 558, -821, -599, 328, 753, -50,
             -568, 268, -92, 381, -96, 730, 629, 678, -837, 351, 896, 63,
             -85, 437, -453, -991, 294, -384, -628, -529, 518, 613, -319,
             -519, -220, -67, 834, 619, 802, 207, 946, -904, 295, 718, -740,
             -557, -560, 80, 296, -90, 401, 407, 798, 254, 154, 387, 434,
             491, 228, 307, 268, 505, -415, -976, 676, -917, 937, -609, 593,
             -36, 881, 607, 121, -373, 915, -885, 879, 391, -158, 588, -641,
             -937, 986, 949, -321
         ]), [
             -991, -976, -937, -917, -904, -885, -837, -821, -740, -641,
             -628, -609, -602, -599, -568, -560, -557, -529, -519, -453,
             -415, -384, -373, -321, -319, -220, -158, -96, -92, -90, -85,
             -67, -50, -36, -19, 63, 80, 121, 154, 168, 207, 228, 254, 268,
             268, 270, 294, 295, 296, 306, 307, 328, 351, 381, 387, 391,
             401, 407, 434, 437, 491, 505, 518, 558, 588, 593, 607, 613,
             619, 629, 676, 678, 718, 730, 753, 759, 798, 802, 834, 879,
             881, 896, 915, 937, 946, 949, 986
         ])
def test_selection_sort_numbers():
    unsorted = [2, 0, 11, 12, 13, -1]
    assert selection_sort(unsorted) == [-1, 0, 2, 11, 12, 13]
def test_selection_sort_letters():
    unsorted = ['b', 'c', 'a', 'd']
    assert selection_sort(unsorted) == ['a', 'b', 'c', 'd']
Esempio n. 38
0
def sort_anagrams(words):
    words = ["".join(selection_sort(list(word))) for word in words]
    return selection_sort(words)
    def testSelectionSort(self):
        expected = sorted(self.array)

        self.assertEquals(expected, selection_sort(self.array))
Esempio n. 40
0
 def test_selection_sort(self):
     self.assertTrue(is_sorted(selection_sort(create_unsorted(10000))))
Esempio n. 41
0
 def test_selection_sort_sorted(self):
     self.assertTrue(is_sorted(selection_sort(self.sorted)))
    # primeiro elemento da lista eh o tamanho da lista
    n = vetor[0]
    # fazendo uma copia do vetor de entrada para cada algoritmo.
    SS = vetor[1:]
    IS = vetor[1:]
    HS = vetor[1:]
    QS = vetor[1:]
    MS = vetor[1:]
    CS = vetor[1:]
    RS = vetor[1:]
    # dicionario para armazenas o tempo de execucao
    tempo = {}

    # algoritmos
    start = time.time()
    selection_sort(SS)
    tempo['SS'] = (time.time() - start)*1000

    start = time.time()
    insertion_sort(IS)
    tempo['IS'] = (time.time() - start)*1000

    start = time.time()
    heapsort(HS)
    tempo['HS'] = (time.time() - start)*1000

    start = time.time()
    quickSort(QS)
    tempo['QS'] = (time.time() - start)*1000

    start = time.time()
def test_selection_sort_numbers():
	unsorted = [2, 0, 11, 12, 13, -1]
	assert selection_sort(unsorted) == [-1, 0, 2, 11, 12, 13]
 def test_selection_sort_empty_list(self):
     self.assertEqual(selection_sort.selection_sort([]), [])
 def test_selection_sort_1_item(self):
     self.assertEqual(selection_sort.selection_sort([1]),[1])
 def test_selection_sort_4_item(self):
     self.assertEqual(selection_sort.selection_sort([2,1,3,1]),[1,1,2,3])
 def test_selection_sort_2_item(self):
     self.assertEqual(selection_sort.selection_sort([2,1]),[1,2])
def test_selection_sort_letters():
	unsorted = ['b', 'c', 'a', 'd']
	assert selection_sort(unsorted) == ['a', 'b', 'c', 'd']
start_time = time.time()
radix_group_numbers(lst)
end_time = time.time()

print 'radix sort took', end_time - start_time, 'seconds'


start_time = time.time()
merge_sort(lst)
end_time = time.time()

print 'merge sort took', end_time - start_time, 'seconds'


start_time = time.time()
bubble_sort(lst)
end_time = time.time()

print 'bubble sort took', end_time - start_time, 'seconds'


start_time = time.time()
selection_sort(lst)
end_time = time.time()

print 'selection sort took', end_time - start_time, 'seconds'



Esempio n. 50
0
        opt = sys.argv[2]
        #print vetor
    except ValueError:
        print "valores invalidos"
        sys.exit(-1)


if __name__ == "__main__":
    main()

    A = vetor[1:]
    # primeiro elemento da lista eh o tamanho da lista
    n = vetor[0]

    if opt == 'SS':
        saida(selection_sort(A))
    if opt == 'IS':
        saida(insertion_sort(A))
    if opt == 'HS':
        saida(heapsort(A))
    if opt == 'MS':
        saida(topDownMergeSort(A))
    if opt == 'QS':
        saida(quickSort(A))
    if opt == 'CS':
        saida(counting_sort(A))
    if opt == 'RS':
        saida(radix_sort(A))


Esempio n. 51
0
 def test_reverse_order(self):
     items = [9, 8, 7, 6, 5, 4, 3, 2, 1]
     sorted_items = [1, 2, 3, 4, 5, 6, 7, 8, 9]
     self.assertEqual(sorted_items, selection_sort(items))
Esempio n. 52
0
	def testSortedResult(self):
		self.assertEqual(selection_sort(self.lst_sorted), self.lst_sorted, 'Problem with sorting')