"Binary find" in the sorted array, find out the number of occurrences of a given number and other applications

Source: Internet
Author: User
Tags int size

Analysis topic: Arrays are sorted to find out the number of times a given number key appears,

Method 1, the most intuitive method is to iterate over the array, time complexity O (N)

Method 2, can be found with the help of two points, time complexity of O (LOGN)
Find out where key appears at the far left and find out where the rightmost key appears.

size_t getlow (int *array,int size, int k)
{
    size_t left = 0;
    size_t right = size-1;
    while (left < right)
    {
        int mid = (left + right)/2;
        if (Array[mid] >= k) Right
            = mid;
        else left
            = mid + 1;
    }
    return left;
}

size_t getRight (int *array, int size, int k)
{
    size_t left = 0;
    size_t right = size-1;
    while (left < right)
    {
        int mid = (left + right)/2;
        if (Array[mid] <= k) Left
            = mid;
        else right
            = mid-1;
    }
    return right;
}


int main ()
{
    int arr[] = {0, 1, 1, 2, 2, 2, 2, 4, 4, 4};
    int size = sizeof (arr)/sizeof (*ARR);
    size_t left = Getlow (arr, size, 2);
    size_t right = GetRight (arr, size, 2);
    size_t count = right-left + 1;
    return 0;
}

Recursive algorithm
Note: The parameter count is a reference.

void GetCount (int *array,int left,int right, int. k,int& count)
{
    if (left > right)
        return;
    int mid = (left + right)/2;
    if (array[mid] = = k)
    {
        ++count;
        GetCount (array, left, Mid-1, K, count);
        GetCount (Array, mid + 1, right, K, count);
    }
    else if (Array[mid] > K)
        getcount (array, left, Mid-1, K, count);
    else
        GetCount (array, mid + 1, right, K, count);
}

Two-point search

If the interval is open, right = Length,left < Right,right = Mid.

[] closed interval
int binsearch (int arr[],int length, int key)
{ 
    int left = 0;
    int right = Length-1;
    int mid = 0;

    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (Arr[mid] < key) Left
            = mid + 1;
        else if (Arr[mid] > key) Right
            = mid-1;
        else
            return mid;
    }
    return-1;
}

* In an ordered array, find the first numeric upper_bound function greater than k *

int firstgreat (int *arr,int size,int k)
{
    int left = 0, right = size-1;
    while (left <= right)
    {
        int mid = (left + right)/2;
        if (Arr[mid] <= k) Left
            = mid + 1;
        else right
            = mid-1;
    }
    Return left < size? Left:-1;
}

in an ordered array, find the first number greater than or equal to K, the Lower_bound function

int firstgreatorequal (int *arr,int size,int k) {int left = 0, right = size-1;
        while (left <= right) {int mid = (left + right)/2;
        if (Arr[mid] < K) left = mid + 1;
    else right = mid-1; } return left < size?
Left:-1; }

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.