Skip to content

Commit

Permalink
AK: Change pivot selection of dual_pivot_quick_sort
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Maato authored and awesomekling committed Apr 28, 2021
1 parent edfe81b commit 67b0d04
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions AK/QuickSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ namespace AK {
template<typename Collection, typename LessThan>
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;
}

int left_pointer, right_pointer;
if (!less_than(col[start], col[end])) {
swap(col[start], col[end]);
if (size > 3) {
int third = size / 3;
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;
Expand Down Expand Up @@ -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[end], col[g]);

left_pointer = j;
right_pointer = g;
int left_pointer = j;
int right_pointer = g;

dual_pivot_quick_sort(col, start, left_pointer - 1, less_than);
dual_pivot_quick_sort(col, left_pointer + 1, right_pointer - 1, less_than);
Expand Down

0 comments on commit 67b0d04

Please sign in to comment.