1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

AK: Add thresholds to quickselect_inline and Statistics::Median

I did a bit of Profiling and made the quickselect and median algorithms
use the best of option for the respective input size.
This commit is contained in:
Staubfinger 2023-01-08 14:55:54 +01:00 committed by Jelle Raaijmakers
parent 6b9344e86c
commit da1023fcc5
4 changed files with 92 additions and 4 deletions

View file

@ -11,6 +11,9 @@
#include <AK/StdLibExtras.h>
namespace AK {
static constexpr int MEDIAN_OF_MEDIAN_CUTOFF = 4500;
// FIXME: Stole and adapted these two functions from `Userland/Demos/Tubes/Tubes.cpp` we really need something like this in `AK/Random.h`
static inline double random_double()
{
@ -161,9 +164,13 @@ size_t quickselect_inplace(Collection& collection, size_t k, PivotFn pivot_fn)
template<typename Collection>
size_t quickselect_inplace(Collection& collection, size_t k)
{
// By default, lets use middle_element to match `quicksort`
return quickselect_inplace(
collection, 0, collection.size() - 1, k, [](auto collection, size_t left, size_t right, auto less_than) { return PivotFunctions::middle_element(collection, left, right, less_than); }, [](auto& a, auto& b) { return a < b; });
if (collection.size() >= MEDIAN_OF_MEDIAN_CUTOFF)
return quickselect_inplace(
collection, 0, collection.size() - 1, k, [](auto collection, size_t left, size_t right, auto less_than) { return PivotFunctions::median_of_medians(collection, left, right, less_than); }, [](auto& a, auto& b) { return a < b; });
else
return quickselect_inplace(
collection, 0, collection.size() - 1, k, [](auto collection, size_t left, size_t right, auto less_than) { return PivotFunctions::random_element(collection, left, right, less_than); }, [](auto& a, auto& b) { return a < b; });
}
}