"Base algorithm" sort-complex sort of two (find out the number of k large)

Source: Internet
Author: User

The idea of cutting is the most essence of high-speed sequencing. Each cut out of the element K one ranked in the K-bit, so take advantage of this idea we know at least 3 points

1. The cut-out element K must finally be ranked in the K position.

2. The element to the left of K must be smaller or equal than K.

3. The element on the right of K must be larger or equal than K.

So we are able to navigate through these properties to an arbitrary element.

For example, after we partition an array, we get a={5,3,4,2,6,8,10,12,11,9}

A[k]=8, so we know that after the orderly a[5]=8, a[4] must be on the left of 8, a[6] must be on the 8 right

So, we must know that the number 8 is the 5+1 small number in the array. 第10-5 a large number

So we come to the assumption that the number a[k]=x is cut, then X must be the k+1 bit in the array, that is, the k+1 small number.

Assuming that the length of the array is n, then x is the number of n-k large in the array.

Here is the code for the cut

public static int partition (int[] array, int left, int. right) {int i = Left;int J = right + 1;while (true) {while (the more (AR Ray[left], Array[++i])) if (i = = right) Break;while (more (Array[--j], Array[left])) if (j = = left) break;if (i >= j) break;e Xchange (Array, I, j);} Exchange (Array, left, j); return J;}

How do you locate the other elements after cutting?

Suppose we locate the a[k]=x and find that the target element o is larger than x, then find it on the right, left=k+1, assuming that it is smaller than x, then find it on the left. Right=k-1, otherwise the location is successful.

public static Int. Select (int[] array, int k) {int left = 0;int right = Array.length-1;while (left < right) {int J = P Artition (array, left, right); if (J < k) left = j + 1;else if (J > k) right = J-1;elsereturn array[k];} return array[k];}

The following gives the complete code, only for everyone to participate in the test

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 (the more (AR Ray[left], Array[++i])) if (i = = right) Break;while (more (Array[--j], Array[left])) if (j = = left) break;if (i >= j) break;e Xchange (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 = P Artition (array, left, right); if (J < k) left = j + 1;else if (J > k) right = J-1;elsereturn array[k];} return array[k];}



"Base algorithm" sort-complex sort of two (find out the number of k large)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.