Fast Ordering (QuickSort) is a sort of algorithm that is commonly used for high efficiency, which is often mentioned in the interview process. The following is a detailed explanation of his principles, give a Java version of the implementation.
Quick Sort Idea:
Two separate sections are divided by a sequence of data element set RN. One part of the keyword is smaller than the other part of the keyword. The two-part keyword is then sorted again until the individual element is only one, at which point the entire element is set in order.
The process of rapid sequencing-digging a hole to fill a number (this is a very image name), a collection of elements r[low ... high], first take a number (generally r[low]) to do the reference, with R[low] as the benchmark to rearrange all the elements.
All smaller than R[low] put the front, all the larger than R[low] behind, then to R[low] as the demarcation, to r[low ... high] divided into two subsets and, again made divided. Until low >= high.
For example: For R={37, 40, 38, 42, 461, 5, 7, 9, 12} The process of a quick sort is as follows (note: The following table of elements in the description below starts from 0):
Original sequence |
37 |
40 |
38 |
42 |
461 |
5 |
7 |
9 |
12 |
One: High-->low |
12 |
40 |
38 |
42 |
461 |
5 |
7 |
9 |
12 |
One: Low--> high |
12 |
40 |
38 |
42 |
461 |
5 |
7 |
9 |
40 |
Two: High-->low |
12 |
9 |
38 |
42 |
461 |
5 |
7 |
9 |
40 |
Two: Low--> high |
12 |
9 |
38 |
42 |
461 |
5 |
7 |
38 |
40 |
Three: High--> low |
12 |
9 |
7 |
42 |
461 |
5 |
7 |
38 |
40 |
Third: Low-->high |
12 |
9 |
7 |
42 |
461 |
5 |
42 |
38 |
40 |
Four: High--> low |
12 |
9 |
7 |
5 |
461 |
5 |
42 |
38 |
40 |
Four: Low--> high |
12 |
9 |
7 |
5 |
461 |
461 |
42 |
38 |
40 |
A trip to sort results |
12 |
9 |
7 |
5 |
37 |
461 |
42 |
38 |
40 |
Start selection Datum base = 37, Table low = 0 at initial position, High = 8 from high=8, start if r[8] < base, write content in high position to R[low], empty high position, low = low + 1;
Start detection from low, because Low=1, R[low] > base, so will R[low] written to R[high], high = high-1;
Low < High is detected, so the first quick sort still needs to be continued:
At this point, low=1,high=7, because R[high] < base, writes R[high] to r[low, low = low + 1;
From low to start probing, low = 2, R[low] >base, so speak R[low] write to r[high],high=high-1;
Continue to detect low less than high
At this time low=2,high=6, empathy R[high] < base, will R[high] written to R[low], low=low+1;
From low to continue probing, low = 3, high=6, R[low] > base, will R[low] written to R[high], high = high-1;
Continue to detect low less than high
At this point, low=3,high=5, empathy R[high] < base, writes R[high] to r[low, low = +1;
From low to continue probing, low = 4,high=5, because R[low] > base, will R[low] written to R[high], high = high-1;
Low = = High = 4 is detected at this point, which is the location where base is located, and the base is written to that location.
Then do a quick sort of sequence Rs1 = {12,9,7,5} and rs2={461,42,38,40} until there is only one element in the RSI, or no elements.
(Note: In the above form you can see that there are some duplicate data in the order (there is no duplicate data in the original data). This is because the data for that location is not cleared, and we look at the memory block's data at a specific time until it is written to the next location- The data at this location is a meaningless dirty data called "pit")
Fast-sorted Java implementations:
Copy Code code as follows:
private static Boolean IsEmpty (int[] n) {
return n = = NULL | | n.length = 0;
}
// ///////////////////////////////////////////////////
/**
* The idea of fast sorting algorithm-digging Pit filling number method:
*
* @param n to be sorted array
*/
public static void QuickSort (int[] n) {
if (IsEmpty (n))
Return
QuickSort (n, 0, n.length-1);
}
public static void QuickSort (int[] n, int l, int h) {
if (IsEmpty (n))
Return
if (L < h) {
int pivot = partion (n, L, h);
QuickSort (N, L, pivot-1);
QuickSort (n, Pivot + 1, h);
}
}
private static int partion (int[] n, int start, int end) {
int tmp = N[start];
while (Start < end) {
while (N[end] >= tmp && start < end)
end--;
if (Start < end) {
n[start++] = N[end];
}
while (N[start] < tmp && Start < end)
start++;
if (Start < end) {
n[end--] = N[start];
}
}
N[start] = tmp;
return start;
}
There is a function in the code:
Copy Code code as follows:
public static void Quicksortswap (int[] n, int l, int h)
This function can be used to sort the data elements between the specific L-H positions in the element set.
About the quick sort that's written here.