First, what is quicksort
Quick Sort Bubble Sort is an improvement
* FIG movable from novice tutorial
Second, implementation
- Taken as a reference number of a start number in the number of columns.
- Partitioning process, than the large full-count put it right, it is less than or equal to the number of full-put it on the left.
- And then the second step is repeated around the interval, until each section is only a number.
Third, the performance parameters on Quick Sort
1, time complexity
Preferably (to be sorted column close disorder):
worst (nearly ordered columns to be sorted):
Average:
2, space complexity
3, is stable
Unstable
4, any suitable type of storage
Sequential storage
Fourth, code implementation
1, for the right to place a large number of reference numbers, eliminate small numbers into the left baseline
There are many ways
As used herein,two pointers
① first A [1] to a temporary storage variable temp, and to make two subscripts left, right end-point to a sequence (e.g., Order left = 1, right = n)
As long as the element pointed right ② A [right] greater than temp, right will continue to the left; when a time A [right] ≤temp when an element A [right] moved to the left pointing element A [left] at.
③ left point as long as the elements A [left] does not exceed temp, will be shifted right and left; when sometime A [left]> temp, the element A [left] moved to the right point to the elements A [right] at .
④ ②③ repeated, until the left and right met, the TEMP (i.e. the original A [1]) into the meet.
Code to achieve this function
// 对于[left,right]区间进行划分
int Partition(int A[], int left, int right){
int temp = A[left]; //将A[left]存放在temp中
while(left < right){ // 只要right 与 right不相遇
while(left < right && A[right] > temp) right--; //反复左移right
A[left] = A[right];
while(left < right && A[right] <= temp) right--; //反复左移right
A[right] = A[left];
}
A[left] = temp; //把temp放到left与right相遇的地方
return left; //返回相遇的位置的下标
}
2, quick sort achieve
void quickSort(int A[], int left, int right){
if(left < right){
int pos = Partition(A, left, right);
quickSort(A, left, pos - 1); //对左子区间递归进行快排
quickSort(A, pos + 1, right); //对右子区间递归进行快排
}
}