Beispiel #1
0
def test_sorted_list():
    data_size = 1000
    seed(42)
    orig_data = sample(range(data_size * 3), k=data_size)

    assert not is_sorted(orig_data)
    test_data = selection_sort(orig_data.copy())
    assert is_sorted(test_data)
    test_data = insertion_sort(orig_data.copy())
    assert is_sorted(test_data)
    test_data = mergesort(orig_data.copy())
    assert is_sorted(test_data)
    test_data = quicksort(orig_data.copy())
    assert is_sorted(test_data)
Beispiel #2
0
  def test_sorted_list(self):
    data_size = 1000
    seed(42)
    orig_data = sample(range(data_size * 3), k=data_size)

    self.assertFalse(is_sorted(orig_data))
    test_data = selection_sort(orig_data.copy())
    self.assertTrue(is_sorted(test_data))
    test_data = insertion_sort(orig_data.copy())
    self.assertTrue(is_sorted(test_data))
    test_data = mergesort(orig_data.copy())
    self.assertTrue(is_sorted(test_data))
    test_data = quicksort(orig_data.copy())
    self.assertTrue(is_sorted(test_data))
Beispiel #3
0
    def q2_is_sorted(self):
        tests = 1000
        points = 0

        for i in xrange(tests / 2):
            l = [random() for i in xrange(randint(20, 50))]
            if not is_sorted(l):
                points += 1
            elif sorted(l) == l:
                points += 1
            if is_sorted(sorted(l)):
                points += 1

        return ('is_sorted', points, tests)
Beispiel #4
0
def compareSpeed(arr, fns=functions):
    for fn in fns:
        s = arr[:]
        start = time.time()
        sortMap[fn](s)
        print(fn + ' : ' + str(time.time() - start))
        s.reverse()
        assert sort.is_sorted(s)
Beispiel #5
0
  def test_sort_times(self):
    data_size = 1000
    seed(42)
    data = sample(range(data_size * 3), k=data_size)

    # selection sort        
    test = data.copy()
    start = perf_counter()
    test = selection_sort(test)
    selection_elapsed_time = perf_counter() - start
    self.assertTrue(is_sorted(test))


    # insertion sort        
    test = data.copy()
    start = perf_counter()
    test = insertion_sort(test)
    insertion_elapsed_time = perf_counter() - start
    self.assertTrue(is_sorted(test))

    # merge sort        
    test = data.copy()
    start = perf_counter()
    test = mergesort(test)
    merge_elapsed_time = perf_counter() - start
    self.assertTrue(is_sorted(test))

    # quick sort        
    test = data.copy()
    start = perf_counter()
    test = quicksort(test)
    quick_elapsed_time = perf_counter() - start
    self.assertTrue(is_sorted(test))

    # tim sort        
    test = data.copy()
    start = perf_counter()
    test.sort()
    tim_elapsed_time = perf_counter() - start

    self.assertLess(merge_elapsed_time, insertion_elapsed_time)
    self.assertLess(quick_elapsed_time, selection_elapsed_time)
    self.assertLess(tim_elapsed_time, merge_elapsed_time)
Beispiel #6
0
def test_sort_times():
    data_size = 1000
    seed(42)
    data = sample(range(data_size * 3), k=data_size)

    # selection sort
    test = data.copy()
    start = perf_counter()
    test = selection_sort(test)
    selection_elapsed_time = perf_counter() - start
    assert is_sorted(test)

    # insertion sort
    test = data.copy()
    start = perf_counter()
    test = insertion_sort(test)
    insertion_elapsed_time = perf_counter() - start
    assert is_sorted(test)

    # merge sort
    test = data.copy()
    start = perf_counter()
    test = mergesort(test)
    merge_elapsed_time = perf_counter() - start
    assert is_sorted(test)

    # quick sort
    test = data.copy()
    start = perf_counter()
    test = quicksort(test)
    quick_elapsed_time = perf_counter() - start
    assert is_sorted(test)

    # tim sort
    test = data.copy()
    start = perf_counter()
    test.sort()
    tim_elapsed_time = perf_counter() - start

    assert merge_elapsed_time < insertion_elapsed_time
    assert quick_elapsed_time < selection_elapsed_time
    assert tim_elapsed_time < merge_elapsed_time
Beispiel #7
0
        self._data[i] = self._data[j]
        self._data[j] = temp


Heap.default_compare = default_compare

# ================== test ===============
if __name__ == '__main__':
    from util import make_random_array
    import sort

    def rcompare(a, b):
        if a < b:
            return 1
        elif a > b:
            return -1
        else:
            return 0

    heap = Heap()
    arr = make_random_array(20, -100, 100)
    for i in arr:
        heap.insert(i)

    res = []
    while not heap.is_empty():
        res.append(heap.pop())

    print(res)
    print(sort.is_sorted(res))
Beispiel #8
0
    result["nodes"] += 1
    _red_scale(node.left, result)
    _red_scale(node.right, result)


# ===================== test =====================
if __name__ == "__main__":
    from util import make_random_array
    from sort import is_sorted

    count = 10
    a = make_random_array(count, 0, count * 10)
    t = RBTree()
    for i in a:
        t.set(i, i)

    assert is_sorted(t.keys())
    assert is_balance(t)

    print('remove: ' + str(t.remove(a[0])))
    print('remove: ' + str(t.remove(a[int(count / 2)])))
    print('remove: ' + str(t.remove(a[count - 1])))
    print('remove: ' + str(t.remove(min(a))))
    print('remove: ' + str(t.remove(max(a))))
    print('remove:' + str(t.remove(count * 20)))

    assert is_balance(t)
    assert is_sorted(t.keys())

    print(t.get(a[20]))