Quick sorting and quick sorting algorithms
Quick sorting uses the idea of divide and conquer. First, find a benchmark number and divide the number into two parts. One part is larger than the benchmark number, and the other part is smaller than the benchmark number. Then, it can be implemented recursively...
// Quick sorting
# Include <iostream>
Using namespace std;
Void Sort (int * a, int left, int right );
Int main (){
Int n, I;
Cin> n;
Int * p = new int [n];
For (I = 0; I <n; I ++)
Cin> p [I];
Sort (p, 0, n-1 );
For (I = 0; I <n; I ++)
Cout <p [I] <"";
Cout <endl;
Delete [] p;
Return 0;
}
Void Sort (int * a, int left, int right ){
If (left> = right) // condition for loop bounce
Return;
Int key;
Int low = left, high = right;
Key = a [low]; // select a number as the base
While (low While (a [high]> = key & low High --;
A [low] = a [high]; // move the number smaller than the key to the front of the key
While (a [low] <= key & low Low ++;
A [high] = a [low]; // move the number greater than the key to the right of the key
}
A [low] = key; // Insert the key to the center
Sort (a, left, low-1); // recursion on the left of the key
Sort (a, low + 1, right); // recursion on the right of the key
}