1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

AK: Introduce cutoff to insertion sort for Quicksort

Implement insertion sort in AK. The cutoff value 7 is a magic number
here, values [5, 15] should work well. Main idea of the cutoff is to
reduce recursion performed by quicksort to speed up sorting
of small partitions.
This commit is contained in:
Marc Luqué 2022-03-25 17:06:11 +01:00 committed by Linus Groh
parent bbb256e8b5
commit 22f472249d
4 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2022, Marc Luqué <marc.luque@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/InsertionSort.h>
#include <AK/Random.h>
#include <AK/Vector.h>
static constexpr int const n = 10;
static constexpr int const iterations = 10;
static constexpr int const seed = 1337;
TEST_CASE(sorts_ascending)
{
srand(seed);
for (int i = 0; i < iterations; ++i) {
Vector<int, n> ints;
for (int j = 0; j < n; ++j)
ints.append(get_random<int>());
Vector<int, n> ints_copy = ints;
insertion_sort(ints);
insertion_sort(ints_copy);
for (int j = 0; j < n - 1; ++j) {
EXPECT(ints[j] <= ints[j + 1]);
EXPECT_EQ(ints[j], ints_copy[j]);
EXPECT_EQ(ints[j + 1], ints_copy[j + 1]);
}
}
}
TEST_CASE(sorts_decending)
{
srand(seed);
for (int i = 0; i < iterations; ++i) {
Vector<int, n> ints;
for (int j = 0; j < n; ++j)
ints.append(get_random<int>());
Vector<int, n> ints_copy = ints;
insertion_sort(ints, [](auto& a, auto& b) { return a > b; });
insertion_sort(ints_copy, [](auto& a, auto& b) { return a > b; });
for (int j = 0; j < n - 1; ++j) {
EXPECT(ints[j] >= ints[j + 1]);
EXPECT_EQ(ints[j], ints_copy[j]);
EXPECT_EQ(ints[j + 1], ints_copy[j + 1]);
}
}
}