1. Brief Introduction
Assume that the array to be sorted is int array [] and the array length is N. Quick sorting is a recursive method.
When N = 1, stop sorting.
When n> 1, first select an element in the array as the benchmark, and then place the element smaller than the benchmark to the left of the benchmark, place the element greater than this benchmark to the right of this element.
The specific implementation is as follows: Suppose array [0] is used as the benchmark:
First, store the benchmark value, int TMP = array [0];
Then, two pointers, int left = 0; int right = n-1;
Bool flag_left = false; // start from the right
While (left <right ){
If (flag_left = true ){
If the current value is less than the benchmark value, left ++;
Otherwise, array [right] = array [left]; right --; flag_left = false;
}
Else {
If the current value is greater than the benchmark, right --;
Otherwise, array [left] = array [right]; left ++; flag_left = true;
}
}
Array [left] = tmp;
In the above Code, when the left pointer is executed, the right pointer can be used. When the right pointer is executed, the left pointer can be used.
Initially, array [0] is backed up, and the left pointer can be used, so it starts from the right.
Finally, recursively sort the arrays in array [0]-array [left-1] and array [left + 1]-array [n-1.
2. Complexity
The average time complexity is O (N * logN ). When the array itself is ordered, the number of comparisons is N * N. To solve this problem, you can use random sorting, that is, each benchmark is selected immediately.
Stability is a non-stable sorting. Because the exchange in the comparison with the benchmark may break the existing order.
3. Code
Void quick_sort (int array [], int n ){
If (n <= 1)
Return;
Int tmp = array [0];
Int left = 0; int right = n-1;
Bool left_flag = false;
While (left <right ){
If (left_flag = true) {// current left pointer
If (array [left] <tmp ){
Left ++;
}
Else {
Array [right] = array [left];
Right --;
Left_flag = false;
}
}
Else {// current right pointer
If (array [right]> tmp ){
Right --;
}
Else {
Array [left] = array [right];
Left ++;
Left_flag = true;
}
}
}
Array [left] = tmp;
Quick_sort (array, left); // array [0]-array [left-1]
Quick_sort (array + left + 1, n-left-1); // array [left + 1], array [n-1]
}
In fact, quick sorting is proposed as an improvement of Bubble sorting. Many implementations are implemented through element exchange. The above code is improved and more efficient, converts an element exchange to an element assignment.
In general, swap sorting includes Bubble sorting and quick sorting.
The following is an implementation of using swap elements for quick sorting:
Void quicksort (int arr [], int beg, int end ){
If (end> = beg + 1 ){
Int piv = arr [beg], k = beg + 1, r = end; // scan from the element on the right of the reference (array [0, for elements smaller than the benchmark, the elements larger than the benchmark are switched to the right position to reduce the right position. // When the switch position on the right overlaps with the current element, the loop ends.
While (k <r) {if (arr [k] <piv)
K ++;
Else
Swap (& arr [k], & arr [r --]);
} // Exchange benchmark
If (arr [k] <piv) {swap (& arr [k], & arr [beg]);
Quicksort (arr, beg, k );
Quicksort (arr, r, end );
} Else {
If (end-beg = 1)
Return;
Swap (& arr [-- k], & arr [beg]);
Quicksort (ARR, beg, k );
Quicksort (ARR, R, end );
}}
}
4. References
Wikipedia-quick sorting http://en.wikipedia.org/wiki/Quicksort