C++ Program to Perform Uniform Binary Search



In this article, we have a sorted array of integers. Our task is to perform a uniform binary search to search for a target element.

What is Uniform Binary Search?

The uniform binary search is an improved version of the binary search algorithm where we use a pre-computed lookup table instead of calculating the middle element every time. The lookup table has elements that are powers of 2, starting from the larger value to 0, to decide the step for next iteration. The time complexity for uniform binary search is O(log n).

Consider the following example scenarios to understand the concept better:

Scenario 1

Input: arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, target = 2
Output: 2 found at index 1

Scenario 2

Input: arr = {4, 6, 9, 12, 15}, target = 15
Output: 15 found at index  4

Steps to Perform Uniform Binary Search

Following are the steps to search the target element in a sorted array using uniform binary search:

  • Define a ubs() function that accepts the given array arr, its size n, and the target value.
  • In the ubs() function, create a lookup table where we store the jump sizes as powers of 2, starting from the largest power of 2 and less than n.
  • The lookup table saves us from calculating middle element again and again, like in a normal binary search.
  • Set the index and step to 0 to start from the beginning. Using a while loop, we iterate through the lookup table until lookup[step] becomes 0.
  • In each iteration, we get the jump size and check if moving to a new index (idx + jump) is valid and the element is less than or equal to the target using if condition.
  • Update the index idx by adding jump value. If the above condition is satisfied, then move to the next smaller jump size by increasing the step by 1.
  • For a valid index idx, check if the element at the idx is equal to the target. If it is equal to target, return the index idx.

C++ Program to Perform Uniform Binary Search

Here is the code implementation of the above-mentioned steps for searching an element using uniform binary search:

#include <iostream>
#include <vector>
using namespace std;

const int MAX_SIZE = 1000;

int lookup[MAX_SIZE];

void lookupTable(int n) {
    int step = 1;
    while (step < n) {
        step <<= 1; 
    }
    step >>= 1;     

    int i = 0;
    while (step > 0) {
        lookup[i++] = step;
        step >>= 1; 
    }
    lookup[i] = 0; 
}

int ubs(int arr[], int n, int target) {
    lookupTable(n);

    int idx = 0, step = 0;
    while (lookup[step] != 0) {
        int jump = lookup[step];
        
        if (idx + jump < n && arr[idx + jump] <= target) {
            idx += jump; 
        }
        
        step++; 
    }

    if (idx < n && arr[idx] == target) {
        return idx;
    }
    
    return -1; 
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 2;

    cout << "Given array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    cout << "Target Element to search: " << target 
         << endl;

    int result = ubs(arr, n, target);
    if (result != -1)
        cout << target <<" found at index " << result << "\n";
    else
        cout << target <<" not found\n";
    return 0;
}

The output of the above code is as follows:

Given array: 1 2 3 4 5 6 7 8 9 10 
Target Element to search: 2
2 found at index 1

The time and space complexity of the above code is O(log n) and O(log n), respectively.

Updated on: 2025-08-18T16:07:46+05:30

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements