The basic idea of fast sorting:
Divide and conquer, that is, decomposition, solution, and combination.
Decomposition:
In the unordered zone R [low .. in high], select a record as the benchmark (usually the first record is selected and recorded as the benchmark, and its subscript is pivotpos). Based on this, the record is divided into two smaller subintervals R [low, pivotpos-1] and R [pivotpos + 1, high], and make all records in the left subinterval less than or equal to the benchmark record. All records in the right subinterval are greater than or equal to the benchmark record, the benchmark records do not need to be sorted subsequently. The key to division is to determine the location of the benchmark record pivotpos.
Solution:
By recursive call, quick sorting is performed for Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. high ].
Combination:
When the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.
Specific process:
Set the sequence to R [low, high], select the first from it as the benchmark, set it as the benchmark, and then set two pointers, I and j, pointing to the sequence R [low, starting position and ending position of high:
1) Gradually increase I until the keyword greater than limit is found;
2) gradually reduce j until the keyword smaller than or equal to limit is found;
3) If I <j, that is, if the number of elements in R [I, j] is greater than 1, R [I] and R [j] are exchanged;
4) Place the benchmark record cursor to the appropriate position, that is, the position pointing to both I and j, and the position is the new pivotpos.
Note:
Quick sorting is unstable, that is, after the same keywords are sorted, the relative position is uncertain.
Sample Code:
Quick Sort code C #
Public class DataStructure_QuickSort
{
// Specify the molecular range and calculate the reference position
Int Partition (int [] arr, int nLower, int nUpper)
{
Int lower = arr [nLower]; // use the first record as the benchmark record.
Int nLeft = nLower + 1; // Add 1. The cursor does not need to be compared with itself.
Int nRight = nUpper;
Int temp;
Do {
While (nLeft <= nRight & arr [nLeft] <= outer) // increase nLeft gradually until a subscript greater than outer is found.
NLeft ++;
While (nLeft <= nRight & arr [nRight]> continue) // gradually decreases from nRight until the subscript smaller than or equal to limit is found.
NRight --;
// When the length (number of elements) of the R [nLeft, nRight] interval is greater than 1, R [nLeft] and R [nRight] are exchanged
If (nLeft <nRight)
{
Temp = arr [nLeft];
Arr [nLeft] = arr [nRight];
Arr [nRight] = temp;
NLeft ++;
NRight --;
}
} While (nLeft <nRight); // stop the loop when nLeft = nRight
// Place the benchmark record cursor in the correct position, that is, the position pointed by both nLeft and nRight
Temp = arr [nLower];
Arr [nLower] = arr [nRight];
Arr [nRight] = temp;
Return nRight;
}
Public void QuickSort (int [] arr, int nLower, int nUpper)
{
Int pivotpos; // reference subscript
If (nLower <nUpper) // sort only when the range length is greater than 1
{
Extends TPOs = Partition (arr, nLower, nUpper); // drag the molecular range and know the benchmark subscript (key to QuickSort)
QuickSort (arr, nLower, pivotpos-1); // recursively sorts the left Interval
QuickSort (arr, pivotpos + 1, nUpper); // recursively sorts the right range
}
}
}