예제 #1
0
    def test_merge_sort(self):
        actual = Utilities.generate_random_array(self.n)
        expected = list(actual)

        actual.sort()
        self.merge.sort(expected)

        self.assertEqual(expected, actual)
        self.assertLess(self.merge.compares,
                        self.n * math.log(self.n) / math.log(2))
예제 #2
0
    def test_quick_sort(self):
        actual = Utilities.generate_random_array(self.n)
        expected = list(actual)

        actual.sort()
        self.quick.sort(expected)

        self.assertEqual(expected, actual)
        self.assertLess(self.quick.swaps,
                        self.n * math.log(self.n) / math.log(2))
예제 #3
0
    def test_insertion_sort_sub_array(self):
        insertion = Insertion()
        input = Utilities.generate_random_array(self.n)

        low = math.floor(0.1 * self.n)
        high = math.floor(0.9 * self.n)
        insertion.sort(input, low, high)

        self.assertTrue(Utilities.is_sorted(input, low, high))
        self.assertFalse(Utilities.is_sorted(input, 0, len(input)))
예제 #4
0
    def test_shell_sort(self):
        shell = Shell()

        actual = Utilities.generate_random_array(self.n)
        expected = list(actual)

        actual.sort()
        shell.sort(expected)

        self.assertEqual(expected, actual)
        self.assertLess(13000, shell.compares)
        self.assertLess(8000, shell.swaps)
예제 #5
0
    def test_selection_sort(self):
        selection = Selection()

        actual = Utilities.generate_random_array(self.n)
        expected = list(actual)

        actual.sort()
        selection.sort(expected)

        self.assertEqual(expected, actual)
        self.assertEqual(499500, selection.compares)
        self.assertEqual(1000, selection.swaps)
예제 #6
0
    def test_insertion_sort(self):
        insertion = Insertion()

        actual = Utilities.generate_random_array(self.n)
        expected = list(actual)

        actual.sort()
        insertion.sort(expected)

        self.assertEqual(expected, actual)
        self.assertLess(insertion.compares, (self.n**2 - self.n) / 2)
        self.assertLess(insertion.swaps, (self.n**2 - self.n) / 2)
예제 #7
0
    def test_linked_list_merge_sort(self):
        list = LinkedList()

        array = Utilities.generate_random_array(self.n)
        for element in array:
            list.insert_end(element)

        list = self.merge.linkedlist_sort(list)

        prev = -sys.maxsize - 1
        while not list.is_empty():
            front = list.remove_front()
            self.assertLessEqual(prev, front)
            prev = front
예제 #8
0
 def test_insertion_sort_cutoff(self):
     self.quick.sort(Utilities.generate_random_array(self.n))
     self.assertNotEqual(0, self.quick.insertion.swaps)
     self.assertNotEqual(0, self.quick.insertion.compares)