Knowee
Questions
Features
Study Tools

4. Inbuilt binary search functions in C++

Question

  1. Inbuilt binary search functions in C++
🧐 Not the exact question you are looking for?Go ask a question

Solution

C++ Standard Template Library (STL) provides several inbuilt functions to perform binary search on arrays and containers. Here are some of them:

  1. binary_search(start, end, num): This function returns boolean true if the element is found in the range [start, end), otherwise returns false.

  2. lower_bound(start, end, num): This function returns an iterator pointing to the first element in the range [start, end) which has a value not less than ‘num’. It returns end if no such element exists.

  3. upper_bound(start, end, num): This function returns an iterator pointing to the first element in the range [start, end) which has a value greater than ‘num’. It returns end if no such element exists.

Here is an example of how to use these functions:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int arr[] = {1, 3, 4, 4, 6, 6};
    int n = sizeof(arr)/sizeof(arr[0]);

    // Check if 4 is present
    if (binary_search(arr, arr+n, 4))
       cout << "4 is present"; 
    else
       cout << "4 is not present"; 

    // Find first occurrence of 4
    auto lb = lower_bound(arr, arr+n, 4);
    cout << "First occurrence of 4 is at index " << (lb - arr);

    // Find last occurrence of 4
    auto ub = upper_bound(arr, arr+n, 4);
    cout << "Last occurrence of 4 is at index " << (ub - arr - 1);

    return 0;
}

In this example, binary_search checks if 4 is present in the array. lower_bound finds the first occurrence of 4, and upper_bound finds the last occurrence of 4.

This problem has been solved

Similar Questions

4. Inbuilt binary search functions in C++

Write a C program to Search a Key string using Binary search TechniqueWrite a program to search a key string in the given array of strings using binary search.At the time of execution, the program should print the message on the console as:Enter value of n : For example, if the user gives the input as:Enter value of n : 4Next, the program should print the messages one by one on the console as:Enter string for a[0] : Enter string for a[1] : Enter string for a[2] : Enter string for a[3] : if the user gives the input as:Enter string for a[0] : AppleEnter string for a[1] : OrangeEnter string for a[2] : KiwiEnter string for a[3] : MangoNext, the program should print the message on the console as:Enter key string : if the user gives the input as:Enter key string : Kiwithen the program should print the result as:After sorting the strings in the array areValue of a[0] = AppleValue of a[1] = KiwiValue of a[2] = MangoValue of a[3] = OrangeThe key string Mango is found at the position 2Similarly if the key element is given as Litchi for the above case then the program should print the output as "The key string Litchi is not found in the array".Fill in the missing code so that it produces the desired result.Sample Test CasesTest Case 1:Expected Output:Enter·value·of·n·:·4Enter·string·for·a[0]·:·AppleEnter·string·for·a[1]·:·BananaEnter·string·for·a[2]·:·OrangeEnter·string·for·a[3]·:·MangoEnter·key·string·:·MangoAfter·sorting·the·strings·in·the·array·areValue·of·a[0]·=·AppleValue·of·a[1]·=·BananaValue·of·a[2]·=·MangoValue·of·a[3]·=·OrangeThe·key·string·Mango·is·found·at·the·position·2

In which case is binary search not applicable?

Write a single C program to implement Linear and binary search in two different functions. Plot the execution_time/input_size plot for both these functions for at least 10 input instances.

Write a function that performs binary search on a sorted array.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.