Recursive Binary Search in C

Intermediate
6 min read
Updated: Nov 2026
1 example

The Recursive Version

The recursive form passes the current range lo and hi as parameters. The base cases are an empty range (lo > hi, not found) and a match. The recursive case calls itself on one half. Its depth is O(log n), so stack use is small, but the iterative version is slightly faster and uses no stack.

Remember
int bs(const int a[], int lo, int hi, int key) {
    if (lo > hi) return -1;              /* base: empty */
    int mid = lo + (hi - lo) / 2;
    if (a[mid] == key) return mid;       /* base: found */
    if (a[mid] < key) return bs(a, mid + 1, hi, key);
    return bs(a, lo, mid - 1, key);
}

Cheat Sheet

Keep these facts handy while you work with Recursive Binary Search in C in C.

TopicDetail
Base 1Empty range returns -1
Base 2Match returns index
DepthO(log n)
Tail callBoth recursive calls are in tail position

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Comparing Searching Algorithms in C.

Example 1 — Count the calls

A static counter records the recursion depth.

C
#include <stdio.h>

static int calls;

static int bs(const int a[], int lo, int hi, int key) {
    calls++;
    if (lo > hi) return -1;
    int mid = lo + (hi - lo) / 2;
    if (a[mid] == key) return mid;
    if (a[mid] < key) return bs(a, mid + 1, hi, key);
    return bs(a, lo, mid - 1, key);
}

int main(void) {
    int a[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};

    calls = 0;
    int r = bs(a, 0, 9, 23);
    printf("index of 23: %d (%d calls)\n", r, calls);

    calls = 0;
    r = bs(a, 0, 9, 24);
    printf("index of 24: %d (%d calls)\n", r, calls);
    return 0;
}

How It Works

  1. For 23 the ranges are 0-9, 5-9, and 5-6, which is three calls.
  2. For 24 the search also visits 6-6 and then the empty range 6-5.
  3. The final empty range hits the base case and returns -1.

Edge Cases & Pitfalls

Check these before relying on Recursive Binary Search in C in real programs.

base

Missing empty-range base case

Without lo > hi, a missing key causes infinite recursion.

mid

Wrong recursive bounds

Recurse on mid + 1 or mid - 1, never mid itself.

Key Takeaways

  • Range as parameters.
  • Two base cases.
  • Depth O(log n).

One line: The recursive form passes the current range lo and hi as parameters.

Frequently Asked Questions

They have the same time complexity. Iterative is slightly faster and uses O(1) space; recursive is often considered clearer.
When lo exceeds hi the key is absent; when a[mid] equals the key it is found.
Continue with "Comparing Searching Algorithms in C" next. Count comparisons for linear and binary search on the same data. The C Searching Algorithms hub lists every topic in order.

Next: Comparing Searching Algorithms in C

Count comparisons for linear and binary search on the same data.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful