Stas's blog: All you need to know about QuickSort
Quicksort performance is gained by the main loop which tends to make excellent usage of CPU caches. Another reason of popularity is that it doesn't need allocation of additional memory.
Quicksorting – 3-way and Dual Pivot
Single Pivot
http://blog.csdn.net/williamcs/article/details/8481137
Three-way partitioning
The way to get around that problem is three-way-partitioning. As a result of such partitioning, elements which are equal to the pivot are put in the middle of the array. Elements which are bigger than pivot are put in the right side of the array and ones which are smaller on the left side, appropriately.
Implementation of that partitioning method consists of two stages. In the first stage arrays is scanned by two pointers ("i" and "j") which are approaching in opposite directions. Elements which are equals to pivot are moved to the ends of array:
It can be seen that after the first stage elements which are equal to the pivot are located on the edges of the array. On the second stage these elements are moved to the middle. That is now their final position and they can be be excluded from the further sorting:
After implementation of such algorithm partitioning function is getting much more complicated. In that implementation the result of the partitioning is lengths of two bound partitions:
http://blog.csdn.net/williamcs/article/details/8481137
First code picks up two pivots. If pivots are the same, it means we have just one pivot and in that case we can used three-way method for partitioning. If pivots are different, then partitioning process will look like this:
Scanning pointer "p" is moving from the beginning of array. If current element is "<> pivot1", then r-th element is swapped with p-th and "r" pointer is moved to next element backwards. All stops when "p" becomes less than "r". After partitioning, array will look like this:
When partition is finished, algorithms is called recursively for each partition.
优势:当系统递归栈的开销所占递归处理本身的比例较高时,快速排序性能较低,从而可以使用直接排序而非递归处理小子文件;
三元中值划分法表示一种抽样估算的思想,也就是使得划分元素尽量接近序列的中间值,具体的抽样可不限于三个,并且在代码实现的时候,需要考虑到循环内部操作的优化,
并且在实现过程中,为了优化栈操作,有的较小序列直接使用插入排序,可以从系统的角度提升算法的性能。
Read full article from Stas's blog: All you need to know about QuickSort
Quicksort performance is gained by the main loop which tends to make excellent usage of CPU caches. Another reason of popularity is that it doesn't need allocation of additional memory.
Quicksorting – 3-way and Dual Pivot
Single Pivot
http://blog.csdn.net/williamcs/article/details/8481137
- public class QuickSort {
- public void sort(Comparable[] a) {
- sort(a, 0, a.length - 1);
- }
- private void sort(Comparable[] a, int lo, int hi) {
- if(lo >= hi) return;
- int piv = partition(a, lo, hi);
- sort(a, lo, piv - 1);
- sort(a, piv + 1, hi);
- }
- private int partition(Comparable[] a, int lo, int hi) {
- int i = lo;
- int j = hi + 1;
- Comparable v = a[lo];
- while(true) {
- while(less(a[++i], v))
- if(i == hi) break;
- while(less(v, a[--j]))
- if(j == lo) break;
- if(i >= j) break;
- swap(a, i, j);
- }
- swap(a, lo, j);
- return j;
- }
- private void swap(Object[] a, int i, int j) {
- Object tmp = a[i];
- a[i] = a[j];
- a[j] = tmp;
- }
- private boolean less(Comparable v, Comparable w) {
- // TODO Auto-generated method stub
- return (v.compareTo(w) < 0);
- }
public static void basicQuickSort(long arr[], int beginIdx, int len) { if ( len <= 1 ) return; final int endIdx = beginIdx + len - 1; final int pivotIdx = getPivotIdx(arr, beginIdx, len); final long pivot = arr[pivotIdx]; Utils.swap(arr, pivotIdx, endIdx); int p = partition(arr, beginIdx, len, pivot); Utils.swap(arr, p, endIdx); basicQuickSort(arr, beginIdx, p-beginIdx); basicQuickSort(arr, p+1, endIdx-p); } public static int partition(long[] arr, int beginIdx, int len, long pivot) { final int endIdx = beginIdx + len - 1; int p = beginIdx; for(int i = beginIdx; i != endIdx; ++i) { if ( arr[i] <= pivot ) { Utils.swap(arr, i, p++); } } return p; } public static int getPivotIdx(long arr[], int beginIdx, int len) { return beginIdx+len/2; }
Three-way partitioning
The way to get around that problem is three-way-partitioning. As a result of such partitioning, elements which are equal to the pivot are put in the middle of the array. Elements which are bigger than pivot are put in the right side of the array and ones which are smaller on the left side, appropriately.
Implementation of that partitioning method consists of two stages. In the first stage arrays is scanned by two pointers ("i" and "j") which are approaching in opposite directions. Elements which are equals to pivot are moved to the ends of array:
It can be seen that after the first stage elements which are equal to the pivot are located on the edges of the array. On the second stage these elements are moved to the middle. That is now their final position and they can be be excluded from the further sorting:
After implementation of such algorithm partitioning function is getting much more complicated. In that implementation the result of the partitioning is lengths of two bound partitions:
public void sort( int [] input, int lowIndex, int highIndex) { |
15 |
16 | if (highIndex<=lowIndex) return ; |
17 |
18 | int lt=lowIndex; |
19 | int gt=highIndex; |
20 | int i=lowIndex+ 1 ; |
21 |
22 | int pivotIndex=lowIndex; |
23 | int pivotValue=input[pivotIndex]; |
24 |
25 | while (i<=gt){ |
26 |
27 | if (less(input[i],pivotValue)){ |
28 | exchange(input, i++, lt++); |
29 | } |
30 | else if (less (pivotValue, input[i])){ |
31 | exchange(input, i, gt--); |
32 | } |
33 | else { |
34 | i++; |
35 | } |
36 |
37 | } |
38 |
39 | sort (input, lowIndex, lt- 1 ); |
40 | sort (input, gt+ 1 , highIndex); |
41 |
42 | } |
43 |
- private void sort(Comparable[] a, int lo, int hi) {
- if(hi <= lo) return;
- int lt = lo, gt = hi;
- Comparable v = a[lo];
- int i = lo;
- while(i <= gt) {
- int cmp = a[i].compareTo(v);
- if(cmp < 0) {
- swap(a, lt++, i++); // same as swap(a, lt, i); lt++, i++;
- } else if(cmp > 0) {
- swap(a, i, gt--);
- } else{
- i++;
- }
- }
- // a[lo..lt-1] < v = a[lt..gt] < a[gt + 1..hi].
- sort(a, lo, lt - 1);
- sort(a, gt + 1, hi);
- }
First code picks up two pivots. If pivots are the same, it means we have just one pivot and in that case we can used three-way method for partitioning. If pivots are different, then partitioning process will look like this:
Scanning pointer "p" is moving from the beginning of array. If current element is "<> pivot1", then r-th element is swapped with p-th and "r" pointer is moved to next element backwards. All stops when "p" becomes less than "r". After partitioning, array will look like this:
When partition is finished, algorithms is called recursively for each partition.
public static void dualPivotQuicksort(long arr[], int beginIdx, int len) { if ( len < 2 ) return; final int endIdx = beginIdx+len-1; long pivot1 = arr[beginIdx]; long pivot2 = arr[endIdx]; if ( pivot1 == pivot2 ) { final long lengths = QuickSort.threeWayPartitioning(arr, beginIdx, endIdx, pivot1); final int lLen = (int)(lengths>>32); final int rLen = (int)lengths; dualPivotQuicksort3(arr, beginIdx, lLen); dualPivotQuicksort3(arr, endIdx-rLen+1, rLen); } else { if ( pivot1 > pivot2 ) { long tmp = pivot1; pivot1 = pivot2; pivot2 = tmp; Utils.swap(arr, beginIdx, endIdx); } int l = beginIdx; int r = endIdx; int p = beginIdx; while ( p <= r ) { if ( arr[p] < pivot1 ) { Utils.swap(arr, l++, p++); } else if ( arr[p] > pivot2 ) { while ( arr[r] > pivot2 && r > p ) { --r; } Utils.swap(arr, r--, p); } else { ++p; } } if ( arr[l] == pivot1 ) ++l; if ( arr[r] == pivot2 ) --r; dualPivotQuicksort3(arr, beginIdx, l-beginIdx); dualPivotQuicksort3(arr, l, r-l+1); dualPivotQuicksort3(arr, r+1, endIdx-r); } }http://www.javacodegeeks.com/2013/06/quicksorting-3-way-and-dual-pivot.html
public class QuickSortDualPivot { |
08 |
09 | public void sort ( int [] input){ |
10 | //input=shuffle(input); |
11 | sort (input, 0 , input.length- 1 ); |
12 | } |
13 |
14 | private void sort( int [] input, int lowIndex, int highIndex) { |
15 |
16 | if (highIndex<=lowIndex) return ; |
17 |
18 | int pivot1=input[lowIndex]; |
19 | int pivot2=input[highIndex]; |
20 |
21 | if (pivot1>pivot2){ |
22 | exchange(input, lowIndex, highIndex); |
23 | pivot1=input[lowIndex]; |
24 | pivot2=input[highIndex]; |
25 | //sort(input, lowIndex, highIndex); |
26 | } |
27 | else if (pivot1==pivot2){ |
28 | while (pivot1==pivot2 && lowIndex<highIndex){ |
29 | lowIndex++; |
30 | pivot1=input[lowIndex]; |
31 | } |
32 | } |
33 |
34 | int i=lowIndex+ 1 ; |
35 | int lt=lowIndex+ 1 ; |
36 | int gt=highIndex- 1 ; |
37 |
38 | while (i<=gt){ |
39 |
40 | if (less(input[i], pivot1)){ |
41 | exchange(input, i++, lt++); |
42 | } |
43 | else if (less(pivot2, input[i])){ |
44 | exchange(input, i, gt--); |
45 | } |
46 | else { |
47 | i++; |
48 | } |
49 |
50 | } |
51 |
52 | exchange(input, lowIndex, --lt); |
53 | exchange(input, highIndex, ++gt); |
54 |
55 | sort(input, lowIndex, lt- 1 ); |
56 | sort (input, lt+ 1 , gt- 1 ); |
57 | sort(input, gt+ 1 , highIndex); |
58 |
59 | } |
60 |
61 | } |
三元中值划分法表示一种抽样估算的思想,也就是使得划分元素尽量接近序列的中间值,具体的抽样可不限于三个,并且在代码实现的时候,需要考虑到循环内部操作的优化,
并且在实现过程中,为了优化栈操作,有的较小序列直接使用插入排序,可以从系统的角度提升算法的性能。
Read full article from Stas's blog: All you need to know about QuickSort