a sort algorithm for high speed province
Is there a sort algorithm that doesn't waste space and can be faster? That's "quick sort". Just listen to the name is not very high-end.
Let's say we're sorting the 9 number of "6 1 2 7 3 4 5 10 8 10" now. First of all, find a number in this sequence as the base number (don't be intimidated by the noun, just a number to refer to, you'll know what it's for later). For convenience, let the first number of 6 be the benchmark. Next, you need to put all the numbers in this sequence that are larger than the datum to the right of 6, and lower the number of the datum to the left of 6, similar to the following arrangement:
3 1 2 5 4 6 9 7 10 8
In the initial state, the number 6 is in the 1th position of the sequence. Our goal is to move 6 to a location in the middle of the sequence, assuming that this position is K. Now we need to look for this k, and with the K-bit as the cutoff point, the number on the left is less than or equal to 6, and the right number is greater than or equal to 6. Think about it, do you have a way to do that?
sorting algorithm for the display of Divinity
The method is simple: "Probe" starts at both ends of the initial sequence "6 1 2 7 9 3 4 5 10 8". First from the right to the left to find a number less than 6, and then from left to right to find a number greater than 6, and then exchange them. Here you can use two variables I and J to point to the left and right side of the sequence respectively. We have a nice name for these two variables "Sentinel I" and "Sentinel J". At the beginning, let Sentinel I point to the leftmost (i.e. i=1) of the sequence, pointing to the number 6. Let Sentinel J point to the rightmost (i.e. =10) of the sequence, pointing to the number.
First, The Sentinel J begins to deploy. Since the base number set here is the leftmost number, it is important to have Sentinel J go first ( think about why ). The Sentry, J, moves to the left (ie j--) one step at a point until he finds a number less than 6 to stop. The Sentinel I then moves to the right (ie i++) one step at a point until a number greater than 6 is found to stop. At last the Sentinel J stopped in front of the number 5, and Sentinel I stopped before the number 7.
Now exchange the values of the elements that Sentinel I and Sentinel J are pointing to. The sequence after the exchange is as follows:
6 1 2 5 9 3 4 7 10 8
By this time, the first exchange ended. The next step is for the Sentinel J to move on to the left (a friendly reminder, every time it has to be sentry J). He found 4 (smaller than the benchmark of 6, satisfying the requirements) and then stopped. Sentry I also continued to move to the right, and he found 9 (6 larger than the benchmark, satisfying the requirements) and then stopped. Swap again at this time, after the exchange of the following sequence:
6 1 2 5 4 3 9 7 10 8
The second swap is over and "probe" continues. Sentry J continued to move to the left, and he found 3 (smaller than the benchmark of 6 to meet the requirements) and then stopped. Sentry I continues to move to the right, bad. When Sentry I and Sentry J met, Sentry I and Sentry J all went to 3. Indicates that the probe ends at this time. We exchanged the benchmark number 6 and 3. The sequence after the exchange is as follows:
3 1 2 5 4 6 9 7 10 8
To this first round of "probing" really ended. At this point, the Datum number 6 is the dividing point, the number on the left of 6 is less than or equal to 6, and the number on the right is greater than or equal to 6. Review the process just now, in fact, Sentry J's mission is to find less than the number of benchmarks, and Sentinel I's mission is to find more than the number of the benchmark, until I and J meet up.
OK, the explanation is complete. Now that the benchmark number 6 has been returned, it is in the 6th place of the sequence. At this point we have divided the original sequence, 6 for the demarcation point split into two sequences, the left of the sequence is "3 1 2 5 4", The right is "9 7 10 8". Then you need to deal with the two sequences separately. Because 6 of the left and right sequences are still very confusing at the moment. But it doesn't matter, we have mastered the method, and then just simulate the method to deal with the 6 left and right sequence respectively.
Java Code Implementation:
public class QUicksortdemo {
public static void Main (string[] args) {
Fast sorting algorithm
int[] Arrays = {6,-2, 12, 1,-1, 2, 11, 7, 9, 3, 4, 11, 5, 5,-4,
13, 10, 8, 0,-6, 19};
arrays = quicksort (0, arrays.length, arrays);
for (int i:arrays) {
System.out.print (i + "");
}
}
public static int[] quicksort (int i, int J, int[] arrays) {
if (j-i = = 1) {
return arrays;
}
if (i >= arrays.length) {
return arrays;
}
int base = arrays[i];//Datum value
int k = i;
int length = j;
for (--j j > i; j--) {
if (Arrays[j] < base) {//the number on the right side of the datum number is smaller than the base number
for (; k < length; k++) {
if (k = = j) {
int temp = Arrays[k];
ARRAYS[K] = arrays[i];
Arrays[i] = temp;
Quickly sort all numbers that are smaller than the base number
Quicksort (k + 1, length, arrays);
//Quick sort for all numbers larger than the Datum
Quicksort (i, k, arrays);
return arrays;
else if (Arrays[k] > base) {
int temp = Arrays[k];
ARRAYS[K] = Arrays[j];
ARRAYS[J] = temp;
Break
}
}
}
}
//When the number on the right side of the reference number is larger than the datum number, the number of numbers that are larger than the datum is sorted quickly
return quicksort (i + 1, length, arrays);
}
}
The results of the operation are as follows:
-6-5-4-2-1 0 1 2 3 4 5 6 7 8 9 10 11 11 12 13 19