[Basic algorithm] sorting-complex sorting 2 (finding the K-th largest number), Algorithm
The idea of segmentation is the most essential part of fast sorting. Each split element K is in the K position, so we should know at least three points using this idea.
1. The split element K must be at the K position.
2. The elements on the left of K must be smaller or equal to K.
3. The elements on the Right of K must be larger or equal to K.
So we can locate any element through these properties.
For example, after we complete an array of partition, we get A = {5, 3}
A [K] = 8, so we know that the sorted A [5] = 8, A [4] must be on the left of 8, and A [6] must be on the Right of 8.
Therefore, we must know that the number 8 is a small number of 5th + 1 in the array, the number 10-5
Therefore, if the split number A [K] = X, then X must be the K + 1 bit in the array, that is, the small number of K + 1.
If the length of the array is N, then X is the maximum number of N-K in the array.
The following is the split code.
public static int partition(int[] array, int left, int right) {int i = left;int j = right + 1;while (true) {while (more(array[left], array[++i]))if (i == right)break;while (more(array[--j], array[left]))if (j == left)break;if (i >= j)break;exchange(array, i, j);}exchange(array, left, j);return j;}
The next step is how to locate other elements after segmentation?
If we locate A [K] = X and find that the target element O is larger than X, we can find it on the right. left = K + 1. If it is smaller than X, then find on the left, right = K-1, otherwise positioning successful
public static int select(int[] array, int k) {int left = 0;int right = array.length - 1;while (left < right) {int j = partition(array, left, right);if (j < k)left = j + 1;else if (j > k)right = j - 1;elsereturn array[k];}return array[k];}
The complete code is provided below for your reference only.
// comparepublic static boolean more(int v, int w) {return v > w;}// exchangepublic static void exchange(int[] array, int i, int j) {int temp = array[i];array[i] = array[j];array[j] = temp;}public static int partition(int[] array, int left, int right) {int i = left;int j = right + 1;while (true) {while (more(array[left], array[++i]))if (i == right)break;while (more(array[--j], array[left]))if (j == left)break;if (i >= j)break;exchange(array, i, j);}exchange(array, left, j);return j;}public static int select(int[] array, int k) {int left = 0;int right = array.length - 1;while (left < right) {int j = partition(array, left, right);if (j < k)left = j + 1;else if (j > k)right = j - 1;elsereturn array[k];}return array[k];}
In the worst case of the following sort algorithms, the lowest time complexity is A bubble sort B insert C select D fast rank
1. Select sorting: unstable, time complexity O (n ^ 2)
The basic idea of sorting is to process the order record sequence for n-1 times. The I-times processing is to swap the smallest person in L [I. n] with L [I. In this way, after I times, the position of the previous I record is correct.
2. Insert sorting: stable, time complexity O (n ^ 2)
The basic idea of insertion sorting is that after I-1 processing, L [1 .. I-1] has arranged the order. I-repeat only inserts L [I] into the proper position of L [1 .. I-1] so that L [1 .. I] is a sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L [I] and L [I-1], if L [I-1] ≤ L [I], then L [1 .. i] the order has been sorted, the I-th processing is over; otherwise, the position of switching L [I] and L [I-1] continues to compare L [I-1] and L [I-2], until a position j (1 ≤ j ≤ i-1) is found so that L [j] ≤ L [j + 1. Figure 1 demonstrates the insertion sorting process for the four elements, which requires three inserts: (a), (B), and (c.
3. Bubble Sorting: stable, time complexity O (n ^ 2)
The Bubble sorting method is the simplest sorting method. The basic idea of this method is to regard the elements to be sorted as vertical "Bubbles", and the smaller elements are relatively light, so as to move up and down. In the Bubble sorting algorithm, we need to process the "bubble" sequence several times. The so-called one-time processing is to check the sequence from the bottom up, and always pay attention to whether the order of two adjacent elements is correct. If the order of two adjacent elements is incorrect, that is, the "light" elements are located below, and their positions are exchanged. Obviously, after processing it again, the "lightest" element floated to the highest position; after processing it twice, the "minor" element floated to the lower position. During the second processing, you do not have to check because the element at the highest position is already the lightest element. In general, the I-th processing does not have to check the elements above the I-th high position, because they are sorted correctly after the processing of the previous I-1 times.
4. Heap sorting: unstable, time complexity O (nlog n)
Heap sorting is A kind of tree-based sorting. During the sorting process, A [n] is considered as A Complete Binary Tree sequential storage structure, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.
5. Merge Sorting: stable, time complexity O (nlog n)
There are two ordered (ascending) sequences stored in the adjacent positions of the same array. It may be set to A [l .. m], A [m + 1 .. h], merge them into an ordered series, and store them in A [l .. h].
6. Fast sorting: unstable, with the best time complexity O (nlogn) and the worst time O (n ^ 2)
Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.
For more information about the time complexity of sorting, see
Which of the following sorting algorithms does not affect the time complexity of the algorithm ()
D
No matter what the original array looks like, each time you traverse the remaining number on one side to select the maximum (minimum)