Algorithm Design: how to calculate the number I or I in the array (continued)

Source: Internet
Author: User

Http://blog.csdn.net/yanzi1225627/article/details/8111806, http://www.eyeandroid.com/thread-9629-1-1.htmlthis article discusses second thinking. What should I do if I am the largest?
First clear the concept, if there is an array a [6] = 2 60 10 32 84 6; then the maximum number of 1st is 84, 2nd is 60, 3rd is 32, 1st is 2, and the second is 6 ., the third is 10, and the third is 32. that is, if I is smaller, it is equivalent to n-I + 1. If the I-th value is greater than the N + 1-I value, the two can be exchanged.

The idea is to sort the data quickly. In order to make it easier for us to find a small number of I.

First, let's look at the Partition Function of quick sorting:

int partition(int a[], int i, int j){int pivot = a[i];while(i<j){while(i<j && a[j] >= pivot)j--;if(i<j)a[i++] = a[j];while(i<j && a[i] <= pivot)i++;if(i<j)a[j--] = a[i];}a[i] = pivot;return i;}

Below is the number of small I:

int find_n_min(int a[], int low, int high, int n_min){int p = partition(a, low, high);if(p-low+1 == n_min)return a[p];if(p-low+1 > n_min) return find_n_min(a, low, p-1, n_min);else return find_n_min(a, p+1, high, n_min-(p-low+1));}

To explain the idea, first find the benchmark index P, and the left side of P is smaller than a [p, then we can obtain that a [p] is the small number of p-low + 1. For example, after a partition function, the array is 5, 3, 6, 12, 50, 21, P = 3, that is, a [3] = 12 is the benchmark of the Division, then a [3] must be a 3-0 + 1 = 4, a small number of 4th.

If P-low + 1> n_min, for example, p-low + 1 = 5, a [p] is a small number of 5th, and the program asks for a small number of 2nd, in this case, we need to recursively look for a number smaller than 2nd in the range of [low, P-1]. This example is not hard to understand.

If the corresponding value is less than n_min, call the number from p + 1 to the high interval to search for a small value of n_min-(p-low + 1.

Pay attention to the following two things,

First, do not forget return before recursion.

Second, when recursion is performed, the new interval must be [low, P-1]. This subtraction must not be less! Why? Here we use a [low] as the reference value, while http://blog.csdn.net/yanzi1225627/article/details/8109035 (high
+ Low)/2 is used as the reference value. Therefore, during recursion, one side of the critical interval is not subtracted.

To clearly illustrate this problem, let's take an example. Assume that the initial array a [] = 12 3 6 5 50 21, we want to find a small number of 3rd.

After entering the partition function for the first time, take 12 as the benchmark, the partition function returns P = 3, array this time is updated to 5 3 6 12 50 21, because p-0 + 1 = 4 is greater than 3, so you can search for indexes from 0 to 2! If the above critical range is not reduced by 1, that is, the search from 0 to 3 (the array is 5, 3, 6, 12), what are the consequences?

The second entry into the partion, the reference is 5, the function returns p = 1, the array is updated to 3 5 6 12, because the p-0 + 1 = 2, so need to enter the third partition, if the critical interval does not contain 1 (correct should start with p + 1), that is, from P (that is, 1) to 3, the array is 5 6 12, the problem is to find a 1st small number in 5 6 12.

The third time we enter partion, the worst is here, because we use the first number 5 as the benchmark, so p = 1 returned by this partition. that is, 5, so the third small number of the final function is 5. this is obviously wrong! Therefore, if a [low] is used as the benchmark, the critical interval must be reduced by 1. If you use mid at http://blog.csdn.net/yanzi1225627/article/details/8109035
= (Low + high)/2 basis, the updated range does not need to be reduced by one.

At this point, the problem of finding small I in the array is solved. If you want to find a large number of I, you only need to convert the parameter to the next one. This is explained at the beginning. You can also change p-low + 1 in the above function to high-p + 1, and a [p] to the I-largest number. simply adjust it.

 

 

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.