Skip to content

Commit f1473fa

Browse files
committed
rollback quicksort to random pivot version
1 parent 1400e7c commit f1473fa

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

include/quick_sort.h

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,42 @@
1919
#define __QUICKSORT_H__
2020

2121
#include <generic.h>
22-
#include <cassert>
2322

2423
namespace alg {
25-
26-
/**
27-
* Return median of begin, middle, and end.
28-
* Order these and hide the pivot.
29-
*/
30-
template<typename T>
31-
static const T & __median3(T list[], int begin, int end) {
32-
assert(begin + 2 <= end);
33-
int middle = end - (end - begin) / 2;
34-
if (list[middle] < list[begin])
35-
swap(list[middle], list[begin]);
36-
if (list[end] < list[begin])
37-
swap(list[end], list[begin]);
38-
if (list[end] < list[middle])
39-
swap(list[end], list[middle]);
40-
41-
//Place pivot at position [end - 1]
42-
swap(list[middle], list[end - 1]);
43-
return list[end - 1];
44-
}
45-
4624
/**
4725
* the quick-sort partition routine
4826
*/
4927
template<typename T>
5028
static int __partition(T list[],int begin, int end) {
51-
T pivot = __median3<T>(list, begin, end);
52-
53-
int i = begin;
54-
int j = end - 1;
55-
56-
while(i < j) {
57-
while(list[++i] < pivot) {}
58-
while(pivot < list[--j]) {}
29+
int pivot_idx = RANDOM(begin,end);
30+
T pivot = list[pivot_idx];
31+
swap(list[begin], list[pivot_idx]);
32+
33+
int i = begin + 1;
34+
int j = end;
35+
36+
while(i <= j) {
37+
while((i <= end) && (list[i] <= pivot))
38+
i++;
39+
while((j >= begin) && (list[j] > pivot))
40+
j--;
5941
if(i < j)
6042
swap(list[i],list[j]);
6143
}
6244

63-
swap(list[i],list[end - 1]);
64-
return i; // final pivot position
45+
swap(list[begin],list[j]);
46+
return j; // final pivot position
6547
}
6648

6749
/**
6850
* quick sort an array of range [begin, end]
6951
*/
7052
template<typename T>
7153
static void quicksort(T list[],int begin,int end) {
72-
if( begin + 1 < end) {
54+
if( begin < end) {
7355
int pivot_idx = __partition<T>(list, begin, end);
7456
quicksort(list, begin, pivot_idx-1);
7557
quicksort(list, pivot_idx+1, end);
76-
} else if ( begin + 1 == end) {
77-
if (list[begin] > list[end])
78-
swap(list[begin], list[end]);
7958
}
8059
}
8160
}

0 commit comments

Comments
 (0)