def test_sort_unsorted_list_of_integers(self):
     """Test that when insertion sort is passed an unsorted list
     of integers that the list is properly sorted.
     """
     insertion_sort(self.unsorted_list_of_integers)
     self.assertEqual(self.unsorted_list_of_integers,
                      self.sorted_list_of_integers)
 def test_sort_sorted_list_of_integers(self):
     """Test that when insertion sort is passed a sorted list
     of integers that the list remains sorted.
     """
     insertion_sort(self.sorted_list_of_integers)
     self.assertEqual(self.sorted_list_of_integers,
                      [-7, -2, -1, 0, 1, 4, 10])
 def test_insertion_sort_is_stable(self):
     """Test that if two elements in a list have the same key,
     that their relative ordering is still preserved after the
     list is sorted.
     """
     insertion_sort(self.unsorted_list_of_products)
     self.assertEqual(self.unsorted_list_of_products,
                      self.sorted_list_of_products_stable)
示例#4
0
def radix_sort(seq):
    n, divisor = len(seq), 10
    min_a, max_a = find_min.find_min(seq), find_max.find_max(seq)
    for i in range(n):
        seq[i] += abs(min_a)
    while divisor / 10 <= max_a:
        blocks = [[] for _ in range(10)]
        for i in range(n):
            digit = seq[i] % divisor - seq[i] % (int(divisor / 10))
            while digit >= 10:
                digit /= 10
            print(seq[i], digit, divisor)
            blocks[int(digit)].append(seq[i])
        j = 0
        for block_i in range(10):
            if blocks[block_i]:
                blocks[block_i] = insertion_sort.insertion_sort(
                    blocks[block_i])
                for k in range(len(blocks[block_i])):
                    seq[j] = blocks[block_i][k]
                    j += 1
        divisor *= 10
    for i in range(n):
        seq[i] += min_a
    return seq
示例#5
0
def flash_sort(seq):
    n, min_a = len(seq), find_min.find_min(seq)
    max_i = 0
    for i in range(n):
        if seq[max_i] < seq[i]:
            max_i = i
    m = int(0.42 * n)
    cft = (m - 1) / (seq[max_i] - min_a)
    dist_table = [0 for _ in range(m)]
    for i in range(n):
        dist_table[int(cft * (seq[i] - min_a))] += 1
    print(dist_table)
    for k in range(1, m):
        dist_table[k] += dist_table[k - 1]
    print(dist_table)
    seq[0], seq[max_i] = seq[max_i], seq[0]
    moves, j = 0, 0
    k = m - 1
    print(seq)
    while moves < n - 1:
        while j > (dist_table[k] - 1):
            j += 1
            k = int(cft * (seq[j] - min_a))
            print(j)
        while j != dist_table[k]:
            k = int(cft * (seq[j] - min_a))
            seq[dist_table[k] - 1], seq[j] = seq[j], seq[dist_table[k] - 1]
            dist_table[k] -= 1
            moves += 1
            print(seq, dist_table)
    return insertion_sort.insertion_sort(seq)
示例#6
0
def test_insertion_sort_empty():
    '''
  Tests insertion sort if given an empty list
  '''

    lst = []

    assert_equals(insertion_sort(lst), [])
示例#7
0
def test_insertion_sort_normal():
    '''
  Tests insertion sort given a normal list
  '''

    lst = [5, 2, 4, 6, 1, 3]
    correct = [1, 2, 3, 4, 5, 6]

    assert_equals(insertion_sort(lst), correct)
示例#8
0
def bucket_sort(sortable, num_of_buckets):
    #Put the numbers into buckets
    the_min = min(sortable)
    the_range = (max(sortable)-the_min)/num_of_buckets
    buckets = [[] for i in range(num_of_buckets)]    
    for x in sortable: 
        bucket_num = (x-the_min)//the_range
        if bucket_num == num_of_buckets: bucket_num -= 1
        buckets[bucket_num].append(x)
    
    #Sort item in buckets and put back into list
    i=0
    for bucket in buckets:
        insertion_sort(bucket)
        for num in bucket:
            sortable[i] = num
            i += 1
    
    return sortable
示例#9
0
def solomon_sort(seq):
    n, min_a, max_a = len(seq), find_min.find_min(seq), find_max.find_max(seq)
    delta = (max_a - min_a) / n
    buckets = [[] for _ in range(n)]
    print(delta)
    for i in range(n):
        new_index = (seq[i] - min_a) / delta if (seq[i] - min_a) / delta > 1 else 1
        buckets[int(new_index) - 1].append(seq[i])
    j = 0
    for bucket_i in range(n):
        if buckets[bucket_i]:
            buckets[bucket_i] = insertion_sort.insertion_sort(buckets[bucket_i])
            for i in range(len(buckets[bucket_i])):
                seq[j] = buckets[bucket_i][i]
                j += 1
    return seq
示例#10
0
def solomon_sort(seq):
    n, min_a, max_a = len(seq), find_min.find_min(seq), find_max.find_max(seq)
    delta = (max_a - min_a) / n
    buckets = [[] for _ in range(n)]
    print(delta)
    for i in range(n):
        new_index = (seq[i] -
                     min_a) / delta if (seq[i] - min_a) / delta > 1 else 1
        buckets[int(new_index) - 1].append(seq[i])
    j = 0
    for bucket_i in range(n):
        if buckets[bucket_i]:
            buckets[bucket_i] = insertion_sort.insertion_sort(
                buckets[bucket_i])
            for i in range(len(buckets[bucket_i])):
                seq[j] = buckets[bucket_i][i]
                j += 1
    return seq
示例#11
0
def draw_chart(sort_type, original_data, frame_interval):
    fig = plt.figure(1, figsize=(16, 9))
    data_set = [Data(d) for d in original_data]
    axs = fig.add_subplot(111)
    plt.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.95)

    if sort_type == 1:
        frames = selection_sort(data_set)
    elif sort_type == 2:
        frames = bubble_sort(data_set)
    elif sort_type == 3:
        frames = insertion_sort(data_set)
    elif sort_type == 4:
        frames = merge_sort(data_set)
    elif sort_type == 5:
        frames = quick_sort(data_set)
    else:
        raise IndexError

    def animate(frame_no):
        # if fi % 2 != 0 and frame_interval < 10 and fi != len(frames)-1:
        #     return
        bars = []
        if len(frames) > frame_no:
            axs.cla()
            axs.set_title(s_type[sort_type])
            axs.set_xticks([])
            axs.set_yticks([])
            bars += axs.bar(list(range(Data.length)),
                            [d.value for d in frames[frame_no]],
                            1,
                            color=[d.color
                                   for d in frames[frame_no]]).get_children()
        frame_no += 1
        return bars

    anim = animation.FuncAnimation(fig,
                                   animate,
                                   frames=len(frames),
                                   interval=frame_interval,
                                   repeat=False)
    return plt, anim
示例#12
0
def bucket_sort(v):
    n = len(v)
    B = []
    for i in range(0, n):
        B.append([])

    for i in range(0, n):
        idx = math.floor(n * v[i])
        B[idx].append(v[i])

    for i in range(0, n):
        B[i] = insertion_sort(B[i])

    idx = 0
    for i in range(0, n):
        for j in range(0, len(B[i])):
            v[idx] = B[i][j]
            idx += 1

    return v
示例#13
0
def bucket_sort(seq, num_of_buckets=10):
    n = len(seq)
    min_a = find_min.find_min(seq)
    for i in range(n):
        seq[i] += abs(min_a)
    print(seq)
    buckets = [[] for _ in range(num_of_buckets)]
    max_a = find_max.find_max(seq)
    divider = ceil((max_a + 1) / num_of_buckets)
    print(divider)
    for i in range(n):
        buckets[int(seq[i] // divider)].append(seq[i])
    j = 0
    print(buckets)
    for bucket_i in range(num_of_buckets):
        if buckets[bucket_i]:
            buckets[bucket_i] = insertion_sort.insertion_sort(buckets[bucket_i])
            for k in range(len(buckets[bucket_i])):
                seq[j] = buckets[bucket_i][k]
                j += 1
    for i in range(n):
        seq[i] += min_a
    return seq
示例#14
0
def radix_sort(seq):
    n, divisor = len(seq), 10
    min_a, max_a = find_min.find_min(seq), find_max.find_max(seq)
    for i in range(n):
        seq[i] += abs(min_a)
    while divisor / 10 <= max_a:
        blocks = [[] for _ in range(10)]
        for i in range(n):
            digit = seq[i] % divisor - seq[i] % (int(divisor / 10))
            while digit >= 10:
                digit /= 10
            print(seq[i], digit, divisor)
            blocks[int(digit)].append(seq[i])
        j = 0
        for block_i in range(10):
            if blocks[block_i]:
                blocks[block_i] = insertion_sort.insertion_sort(blocks[block_i])
                for k in range(len(blocks[block_i])):
                    seq[j] = blocks[block_i][k]
                    j += 1
        divisor *= 10
    for i in range(n):
        seq[i] += min_a
    return seq
示例#15
0
def bucket_sort(seq, num_of_buckets=10):
    n = len(seq)
    min_a = find_min.find_min(seq)
    for i in range(n):
        seq[i] += abs(min_a)
    print(seq)
    buckets = [[] for _ in range(num_of_buckets)]
    max_a = find_max.find_max(seq)
    divider = ceil((max_a + 1) / num_of_buckets)
    print(divider)
    for i in range(n):
        buckets[int(seq[i] // divider)].append(seq[i])
    j = 0
    print(buckets)
    for bucket_i in range(num_of_buckets):
        if buckets[bucket_i]:
            buckets[bucket_i] = insertion_sort.insertion_sort(
                buckets[bucket_i])
            for k in range(len(buckets[bucket_i])):
                seq[j] = buckets[bucket_i][k]
                j += 1
    for i in range(n):
        seq[i] += min_a
    return seq
示例#16
0
 def test_merge_sort_empty(self):
     arr_one: List[int] = []
     insertion_sort.insertion_sort(arr_one)
     self.assertEqual([], arr_one)
 def test_insertion_sort(self):
     self.assertEqual(insertion_sort.insertion_sort(self.array),
                      self.sorted_array)
 def test_sort_empty_list(self):
     """Test that when insertion sort is passed an empty list,
     that nothing happens."""
     insertion_sort(self.empty_list)
     self.assertEqual(self.empty_list, [])
示例#19
0
 def test_insertion_sort(self):
     for test, assertion in self.test_cases:
         with self.subTest():
             self.assertEqual(insertion_sort(test), assertion)
示例#20
0
 def test_merge_sort_one(self):
     arr_one: List[int] = [10]
     insertion_sort.insertion_sort(arr_one)
     self.assertEqual([10], arr_one)
示例#21
0
 def test_merge_sort_desc(self):
     expected: List[int] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
     insertion_sort.insertion_sort(arr, True)
     self.assertEqual(expected, arr)
示例#22
0
 def test_merge_sort_asc(self):
     expected: List[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     insertion_sort.insertion_sort(arr)
     self.assertEqual(expected, arr)
示例#23
0
def test_sort():
    xs = list(np.random.randint(0, 1000, size=[1000]))
    assert insertion_sort(xs) is None
    assert xs == sorted(xs)