Today, Dapeng brother and everyone learn to exchange sorting in the high-speed sorting.
High-speed sequencing is an improvement to the bubbling sort. The basic idea of it is. By sequencing the backlog to separate two parts, some of the recorded keyword are smaller than the keyword. The two parts of the record can be sequentially sorted to achieve the order of the sequence.
High-speed sequencing basic steps:
STEP1, define two variables low and high, their initial values are low and high, in addition to the other variable PivotKey.
STEP2, first searches forward from high point to find the first keyword less than pivotkey record and PivotKey exchange.
STEP3, search backward from the position indicated by low. Find the first keyword Big fish pivotkey record and PivotKey exchange.
STEP4, repeat the above steps until Low=high.
Pending sequence: 49 38 65 97 76 13 27 49
1. The keyword with low and high and pivot record is PivotKey:
49 38 65 97 76 13 27 49
↑ (Low) ↑ (high)
↑ (PivotKey)
2. Search forward from high point to find the first record less than PivotKey (here 27) and pivot record Exchange:
27 38 65 97 76 13 49 49
↑ (Low) ↑ (high)
↑ (PivotKey)
3, search backward from the point of low, find the first record greater than PivotKey (here is 65) and pivot records exchange each other:
27 38 49 97 76 13 65 49
↑ (Low) ↑ (high)
↑ (PivotKey)
4, repeat 2, 3 steps until Low=high.
27 38 13 97 76 49 65 49
↑ (Low) ↑ (high)
↑ (PivotKey)
27 38 13 49 76 97 65 49
↑ (Low) ↑ (high)
↑ (PivotKey)
27 38 13 49 76 97 65 49
↑ (Low=high)
↑ (PivotKey)
The above gives a trip to the fast-running process, the whole process can be recursive, the above operation is completed, and then the two sub-sequences of the high-speed sequencing.
Java implementations such as the following:
Public class QuickSort {
Public static void main (string[] args) {
// TODO Auto-generatedmethod stub
int [] a={49,38,65,97,76,13,27,49};
int pivotloc,low=0,high=a.length-1;
System. out. Print (" before sorting :");
for (int x:a) {
System. out. Print (x+",");
}
if (Low
Pivotloc = quickSort(A,low,high);
QuickSort (a,low,pivotloc-1);
QuickSort (A,pivotloc+1,high);
}
System. out. Print (" after sorting :");
for (int x:a) {
System. out. Print (x+",");
}
}
private static int quickSort (int[] A,int low, int High ) {
int Pivotkey=a[low];
while (Low
while (Low
high--;
A[low]=a[high];
while (Low
low++;
A[high]=a[low];
}
A[low]=pivotkey;
return low;
}
}
The time complexity for high-speed sequencing is O (NLOGN).
High-speed sorting of exchange sort