Algorithm-Number of times the number appears in the sorted array _ two-point lookup

Source: Internet
Author: User

Topic:
To count the number of occurrences of a number in a sorted array, such as the sort array {1,2,3,3,3,4,5}, the number 3 appears is 3.

Ideas for solving problems:
1. First of all, traversing an array is sure to know the number of a number, at this time the complexity O (n).
2. In addition, we note that the task is essentially to look up the problem, and is a sorted array, you can try the binary lookup algorithm, so that we can find a 3, and then according to the 3 to the array of the two ends of the traversal, find all 3, but if 3 is n. This algorithm is inherently time complex or O (n).
3. Finally, we found that in the sorted array, if we know where the first 3 and the last 3 appear, then we know the number, then we can continue to use the Second Division after the first use of the binary lookup, to find the ends of 3.
Obviously, just don't modify some of the traditional binary lookup rules:
If the median number is greater than 3, then the first and last 3 must be on the left half;

If the median number is less than 3, then the first and last 3 are definitely on the right half;

If the middle number equals 3, then you need to determine if this 3 is the first or last 3:
If the number on the left side of the middle number is 3, then the first 31 is set on the left half:

If the number on the left side of the middle number is not 3, then the first 3 is in the middle:

If the number on the right side of the middle number is 3, then the last 31 is set on the right half:

If the number on the right side of the middle number is not 3, then the last 3 must be in the middle:

So, we can think of finding the first and last two questions, and using two functions to return to the position in the array, then their difference of +1 is the number.

Personal feeling, the key to a binary search is to use a rule, so that the range after each lookup can be halved, once to reduce the time complexity, so the improved binary lookup can be used flexibly in many problems, in addition to this, in the rotating array of the smallest number of problems can also be used, even in the smallest number of rotating arrays, Even the prerequisites for a binary lookup are changed and are no longer an array of orders.

Code implementation

int GETNUMBEROFK (int* data, int length, int k) {int number = 0;
        if (data!= NULL && length > 0) {int-i = GETFIRSTK (data, length, K, 0, length-1);

        int last = GETLASTK (data, length, K, 0, length-1);
    if (> 1 && last >-1) Number = Last-first + 1;
return number;

    //Find the first k int getfirstk (int* data, int length, int k, int start, int end) {if (Start > End) return-1;
    int Middleindex = (start + end)/2;

    int middledata = Data[middleindex]; if (Middledata = = k) {if (Middleindex > 0 && data[middleindex-1]!= k) | | middleinde
        x = = 0) return middleindex;
    else end = MiddleIndex-1;
    else if (Middledata > k) end = MiddleIndex-1;

    else start = Middleindex + 1;
return GETFIRSTK (data, length, K, start, end); //Find the last K int Getlastk (int* data, int length, int k, int Start, int end) {if (Start > End) return-1;
    int Middleindex = (start + end)/2;

    int middledata = Data[middleindex]; if (Middledata = = k) {if (Middleindex < length-1 && Data[middleindex + 1]!= k) | |
        Iddleindex = = length-1) return middleindex;
    else start = Middleindex + 1;
    else if (Middledata < k) Start = Middleindex + 1;

    else end = MiddleIndex-1;
return GETLASTK (data, length, K, start, end); }

GETNUMBEROFK function has nothing to say, is in the call, the rest of the GETFIRSTK and GETLASTK logic is the same, as long as the understanding of a good.
In Getfirstk, recursive methods are used to adjust the array range before the next recursion, so that the next recursion is less than half the scope of this recursion, which is two points.
The conditions for recursive exit are:

if ((Middleindex > 0 && data[middleindex-1]!= k) 
            | | middleindex = = 0)

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.