Example #1
0
    def test_insert_sort(self):
        data = [3, 5, 7, 11, 13, 2, 9, 6]
        output = [2, 3, 5, 6, 7, 9, 11, 13]
        self.assertEqual(output, insert_sort(data=data))

        data = tuple([22, 11, 99, 88, 9, 7, 42])
        output = [7, 9, 11, 22, 42, 88, 99]
        self.assertEqual(output, insert_sort(data=data))
Example #2
0
def bucketsort(A):
    res = []
    length = len(A)
    B = [[] for i in range(length)]
    for i in A:
        B[int(length * i)].append(i)
    for j in B:
        insert_sort(j)
        res.extend(j)
    return res
Example #3
0
def bucketsort(A):
    res = []
    length = len(A)
    B = [[] for i in range(length)]
    for i in A:
        B[int(length * i)].append(i)
    for j in B:
        insert_sort(j)
        res.extend(j)
    return res
Example #4
0
def __merge_sort2(array, left, right):
    if right - left <= 50:
        insert_sort(array, left, right)
        #必须要有return,否则无限递归
        return
    mid = left + (right - left) // 2
    __merge_sort2(array, left, mid)
    __merge_sort2(array, mid + 1, right)
    if array[mid] > array[mid + 1]:
        __merge(array, left, mid, right)
Example #5
0
def test_insert_sort_random_list():
    """Test insert sort on random list of integers."""
    from insert_sort import insert_sort
    x = [randint(0, 9) for p in range(0, 9)]
    s = insert_sort(x)
    x.sort()
    assert s == x
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))])
Example #7
0
def shell_sort_once(unsorted_list, add):
    '''shell_sort_once
    params:
        unsorted_list: 未排序列表
        add:本趟增量
    returns:
        unsorted_list:本轮返回结果
    '''
    for i in range(add):
        unsorted_list[i::add] = insert_sort(unsorted_list[i::add])
        print("SHELL DEBUG:", unsorted_list)
    return unsorted_list
def bucket_sort(arr):
    """桶排序"""
    min_num = min(arr)
    max_num = max(arr)
    bucket_size = (max_num - min_num) / len(arr)  # 桶的大小
    count_list = [[] for _ in range(len(arr) + 1)]  # 桶数组
    # 向桶数组填数
    for i in arr:
        count_list[int((i - min_num) // bucket_size)].append(i)
    arr.clear()
    # 回填,这里桶内部排序调用了 insert_sort()
    print(count_list)
    for i in count_list:
        for j in insert_sort(i):
            arr.append(j)
    return arr
Example #9
0
def test_insert_sort():
    # get data from file
    print("test insert 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 insert_sort import insert_sort

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

    assert (out_data == target_data)
Example #10
0
def test_insert_sort(input_array):
    return insert_sort(input_array)
def insertion_sort(a):
    insert_sort(a)
Example #12
0
def test_simple_list_insert_sort():
    """Test insert sort on basic list."""
    from insert_sort import insert_sort
    s = insert_sort([5, 4, 3, 2, 1])
    assert s == [1, 2, 3, 4, 5]
Example #13
0
 def test_insert_sort(self):
     self.assertEqual(insert_sort(self.RAW_DATA), self.REF_DATA)
Example #14
0
    def test_insert_sort_type(self):
        with self.assertRaises(TypeError):
            insert_sort(data='zfsggf')

        with self.assertRaises(TypeError):
            insert_sort(data=(i for i in xrange(10)))
Example #15
0
def test_insert_sort():
    a = ['d', 'a', 'c', 'b']
    insert_sort(a)
    assert a == ['a', 'b', 'c', 'd']
Example #16
0
__author__ = 'Mercy'
__date__ = '2017-03-10'

import sys

if __name__ == '__main__':

    # 输入
    input_line = sys.stdin.readline().strip()
    array = map(int, input_line.split())

    # 插入类排序

    print '直接插入排序'
    from insert_sort import insert_sort
    insert_sort(array)

    # print '折半插入排序'
    # from BInsert_sort import BInsert_sort
    # BInsert_sort(array)

    # print '希尔排序'
    # from shell_sort import shell_sort
    # shell_sort(array)

    # 交换类排序

    # print '冒泡排序'
    # from bubble_sort import bubble_sort
    # bubble_sort(array)
Example #17
0
def test_insert_sort_with_empty_list():
    """."""
    l_l = LinkedList()
    res = insert_sort(l_l)
    assert res is None
def test_insert_sort_a_simple_sort():
    """Test on a reverse numerical list."""
    insert_sort(bs_list)
    assert sb_list == [1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_insert_sort_numerical_ordered_list():
    """Test sorting on a in order list."""
    insert_sort(sb_list)
    assert sb_list == [1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_empty_list_returns_empty_list():
    """Test if empty list returned given empty list."""
    from insert_sort import insert_sort
    assert insert_sort([]) == []
def test_insert_sort_does_not_accept_tuple():
    """Test if value error raised if tuple passed in func."""
    from insert_sort import insert_sort
    with pytest.raises(TypeError):
        assert insert_sort((8, 9, 1, 3))
Example #22
0
 def test_insert_sort_large(self):
     data = set([random.randint(1, 1000) for i in range(1, 1000)])
     output = sorted(list(data))
     self.assertEqual(output, insert_sort(data=data))
Example #23
0
def test_list_one_element_insert_sort():
    """Test insert sort on one element."""
    from insert_sort import insert_sort
    s = insert_sort([1])
    assert s == [1]
Example #24
0
def test_insert_sort_does_not_mutate_the_original_list():
    """Test_insert_sort_does_not_mutate_the_original_list."""
    t_l = [5, 2, 7, 4, 1]
    l_l = LinkedList(t_l)
    s_l = display(insert_sort(l_l))
    assert s_l is not t_l
Example #25
0
def test_insert_sort_empty_list():
    """Test Insert sort on empty list."""
    from insert_sort import insert_sort
    s = insert_sort()
    assert s == []
def test_letters_sorted_alphabetically():
    """Test if words are sorted alphabetically."""
    from insert_sort import insert_sort
    words = ['toast', 'eggs', 'syrup', 'bacon']
    assert insert_sort(words) == ['bacon', 'eggs', 'syrup', 'toast']
def test_list_of_one_returns_same_list():
    """Test if list of one returns same list."""
    from insert_sort import insert_sort
    assert insert_sort([9]) == [9]
Example #28
0
 def test_insert_sort_empty(self):
     self.assertEqual([], insert_sort(data=[]))
def test_insert_sort_on_out_of_order_list():
    """Test sorting a non numerical list."""
    insert_sort(uo_list)
    assert uo_list == [1, 3, 22, 23, 34, 56, 78, 79, 87]
def test_list_of_two_returns_same_list():
    """Test if list of two swaps."""
    from insert_sort import insert_sort
    assert insert_sort([9, 1]) == [1, 9]
def test_long_list_sorts_in_order():
    """Test if long list prints in correct order."""
    from insert_sort import insert_sort
    assert insert_sort([9, 1, 11, 19, 4, 3, 2,
                        14]) == [1, 2, 3, 4, 9, 11, 14, 19]
def test_insert_sort_does_not_accept_string():
    """Test if value error raised if str passed in func."""
    from insert_sort import insert_sort
    with pytest.raises(TypeError):
        insert_sort('8, 9, 1, 3')
Example #33
0
 def test_insert_sort(self):
     self.assertEqual(insert_sort.insert_sort(self._arr), self._res)
Example #34
0
def test_insert_sort_words(words):
    """Test insertion sort works with words."""
    l_l = LinkedList(words)
    assert display(insert_sort(l_l)) == sorted(words)