How to find the number of times a number appears in a sorted array

Source: Internet
Author: User
Tags include

Topic: Statistics The number of times a number appears in a sorted array.

Through binary find, find the first occurrence of the position, and then find the last occurrence of the position, subtract can be.

Time complexity O (logn).

Code:

* * * main.cpp * * Created on:2014.6.12 * author:spike//*eclipse CDT, gcc 4.8.1*/#incl  Ude <stdio.h> #include <stdlib.h> #include <string.h> 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) | | middleindex = = 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); 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) | | | middleindex = = length-1) r  
        Eturn Middleindex;  
    else start = middleindex+1;  
    else if (Middledata < k) start = middleindex+1;  
    else end = MiddleIndex-1;  
return GETLASTK (data, length, K, start, end);  
    int GETNUMBEROFK (int* data, int length, int k) {int number = 0;  
    if (data = NULL && length <= 0) return number;  
    INT-A-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;  
    int main (void) {int data[] = {1, 2, 3, 3, 3, 3, 4, 5};  
    int k = 3;  
    int result = GETNUMBEROFK (data, 8, k);  
      
    printf ("result =%d\n", result);  
return 0; }

Output:

result = 4

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Author: csdn Blog Caroline-wendy

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.