-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianOf3QuickSort.h
More file actions
23 lines (19 loc) · 903 Bytes
/
MedianOf3QuickSort.h
File metadata and controls
23 lines (19 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <vector>
using namespace std;
// Median of 3 quick sort
// A recursive in-place exchange type sort. This version of quick sort uses a median of 3 partitioning
// scheme at each recursion level to try and create even sized partitions. This will prevent the the
// situation where a sorted input results in the majority of the elements ending up in one partition.
//
// Notes: In many implementations quick sort is only performed until the partition reaches a cut-off (~20)
// size and then an insertion sort is performed over the entire array for the remaining unordered elements.
class MedianOf3QuickSort
{
public:
void Sort(vector<int>& v);
private:
inline void Swap(vector<int>& v, const unsigned i, const unsigned j);
int Median3(vector<int>& v, const unsigned first, const unsigned last);
void Sort(vector<int>& v, const unsigned first, const unsigned last);
};