mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
AK: Change pivot selection of dual_pivot_quick_sort
Picking the first and last elements as pivots makes it so that a sorted array is the worst-case input for the algorithm. This change instead picks pivots at approximately 1/3 and 2/3 in the array. This results in desired performance for sorted arrays. Of course this only changes which inputs result in worst-case performance, but hopefully those inputs occur less frequently than already sorted arrays.
This commit is contained in:
parent
edfe81b1ee
commit
67b0d04315
1 changed files with 17 additions and 6 deletions
|
@ -18,13 +18,24 @@ namespace AK {
|
||||||
template<typename Collection, typename LessThan>
|
template<typename Collection, typename LessThan>
|
||||||
void dual_pivot_quick_sort(Collection& col, int start, int end, LessThan less_than)
|
void dual_pivot_quick_sort(Collection& col, int start, int end, LessThan less_than)
|
||||||
{
|
{
|
||||||
if (start >= end) {
|
int size = end - start + 1;
|
||||||
|
if (size <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int left_pointer, right_pointer;
|
if (size > 3) {
|
||||||
if (!less_than(col[start], col[end])) {
|
int third = size / 3;
|
||||||
swap(col[start], col[end]);
|
if (less_than(col[start + third], col[end - third])) {
|
||||||
|
swap(col[start + third], col[start]);
|
||||||
|
swap(col[end - third], col[end]);
|
||||||
|
} else {
|
||||||
|
swap(col[start + third], col[end]);
|
||||||
|
swap(col[end - third], col[start]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!less_than(col[start], col[end])) {
|
||||||
|
swap(col[start], col[end]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int j = start + 1;
|
int j = start + 1;
|
||||||
|
@ -57,8 +68,8 @@ void dual_pivot_quick_sort(Collection& col, int start, int end, LessThan less_th
|
||||||
swap(col[start], col[j]);
|
swap(col[start], col[j]);
|
||||||
swap(col[end], col[g]);
|
swap(col[end], col[g]);
|
||||||
|
|
||||||
left_pointer = j;
|
int left_pointer = j;
|
||||||
right_pointer = g;
|
int right_pointer = g;
|
||||||
|
|
||||||
dual_pivot_quick_sort(col, start, left_pointer - 1, less_than);
|
dual_pivot_quick_sort(col, start, left_pointer - 1, less_than);
|
||||||
dual_pivot_quick_sort(col, left_pointer + 1, right_pointer - 1, less_than);
|
dual_pivot_quick_sort(col, left_pointer + 1, right_pointer - 1, less_than);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue