Fast sorting, the first step to determine a key value key (typically set to the first element), the basic idea is to put a small number than key on the left side of key will be larger than the number of key on the key to the right, so complete a quick row
Next, the same method is applied to the left and right of the key respectively.
Algorithm steps:
1. Select a benchmark key (the first element is typically selected)
2 set two pointers low and high, initially pointing to the first element and the last element
3. Scan high from right to left until you find an element that is smaller than key, move this element to the low position, and then scan from left to right to find a position that is larger than the key element
The condition for the end of a quick line is Low==high
4. Tail work, insert key into low position
5 to the left side of the low to the right respectively recursion, repeat the above procedure
Algorithm complexity of O (NLOGN)
#include <iostream>;
using namespace Std;
void Qsort (int*list, int left, int. right);
int main () {
int n,i;
while (CIN >> N) {
int *a = new Int[n];
for (i = 0; i < n; i++)
CIN >> A[i];
Qsort (A, 0, n-1);
for (i = 0; i < n; i++)
cout << A[i] << "";
cout << Endl;
return 0;
}
}
void Qsort (int*list, int left, int. right) {
/* Exit Condition */
if (left >= right)
Return
Setting key values
int key = List[left];
Two pointer to left and right scan
int low = left and high = right;
while (Low < high) {/* One scan end flag */
/* Find the number on the right larger than key */
while (List[high]>=key&&high > Low)
high--;
List[low] = list[high];//found and moved to the low end
/* Find the number on the left that is smaller than key */
while (list[low]<= Key&&high>low)
low++;
List[high] = List[low]; Find and move to high end
}
List[low] = key;//The last step to insert K, at this time, a quick line is complete
/* Repeat the above method for two sub-sequences recursively, respectively */
Qsort (list, left, low-1);
Qsort (list, low+1,right);
}
Divide and conquer the fast Platoon