Skip to Content
Support Palestine 🍉  ⬩  In News

Lab 3: Binary Search

Target:

geca-labs/lab-daa-03

OR
Continue with your 👇 existing repo

In this lab, we explore binary search, a fundamental search algorithm for sorted arrays. We implement binary search using both recursive and iterative approaches, analyze their time and space complexities, and provide code examples in Python, C++, and JavaScript.

We demonstrate both:

  • Recursive implementation (divide and conquer)
  • Iterative implementation (using loops)

Mathematical Definition

Given a sorted array A[0n1]A[0 \dots n-1] and a target value xx, binary search recursively or iteratively finds an index ii such that A[i]=xA[i] = x, or reports that xx is not in the array.

Recursive definition:

bs(A,low,high,x)={1if low>highmif A[m]=x, where m=low+high2bs(A,low,m1,x)if x<A[m]bs(A,m+1,high,x)if x>A[m]\text{bs}(A, low, high, x) = \begin{cases} -1 & \text{if } low > high \\ m & \text{if } A[m] = x, \text{ where } m = \lfloor \frac{low + high}{2} \rfloor \\ \text{bs}(A, low, m-1, x) & \text{if } x < A[m] \\ \text{bs}(A, m+1, high, x) & \text{if } x > A[m] \end{cases}

Implementation

binary_search.py
# Recursive binary search def binary_search_recursive(arr, low, high, x): if low > high: return -1 mid = (low + high) // 2 if arr[mid] == x: return mid elif x < arr[mid]: return binary_search_recursive(arr, low, mid - 1, x) else: return binary_search_recursive(arr, mid + 1, high, x) # Iterative binary search def binary_search_iterative(arr, x): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == x: return mid elif x < arr[mid]: high = mid - 1 else: low = mid + 1 return -1 # Driver arr = [1, 3, 5, 7, 9, 11] x = 7 print("Recursive:", binary_search_recursive(arr, 0, len(arr)-1, x)) print("Iterative:", binary_search_iterative(arr, x))

C++

binary_search.cpp
#include <iostream> using namespace std; // Recursive binary search int binary_search_recursive(int arr[], int low, int high, int x) { if (low > high) return -1; int mid = (low + high) / 2; if (arr[mid] == x) return mid; else if (x < arr[mid]) return binary_search_recursive(arr, low, mid - 1, x); else return binary_search_recursive(arr, mid + 1, high, x); } // Iterative binary search int binary_search_iterative(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == x) return mid; else if (x < arr[mid]) high = mid - 1; else low = mid + 1; } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9, 11}; int n = sizeof(arr)/sizeof(arr[0]); int x = 7; cout << "Recursive: " << binary_search_recursive(arr, 0, n-1, x) << endl; cout << "Iterative: " << binary_search_iterative(arr, n, x) << endl; return 0; }

JavaScript

binary_search.js
// Recursive binary search function binary_search_recursive(arr, low, high, x) { if (low > high) return -1; const mid = Math.floor((low + high) / 2); if (arr[mid] === x) return mid; else if (x < arr[mid]) return binary_search_recursive(arr, low, mid - 1, x); else return binary_search_recursive(arr, mid + 1, high, x); } // Iterative binary search function binary_search_iterative(arr, x) { let low = 0, high = arr.length - 1; while (low <= high) { const mid = Math.floor((low + high) / 2); if (arr[mid] === x) return mid; else if (x < arr[mid]) high = mid - 1; else low = mid + 1; } return -1; } // Driver const arr = [1, 3, 5, 7, 9, 11]; const x = 7; console.log("Recursive:", binary_search_recursive(arr, 0, arr.length - 1, x)); console.log("Iterative:", binary_search_iterative(arr, x));

Expected Output

Recursive: 3 Iterative: 3

Both methods return the index 3 since arr[3] = 7.

Time and Space Complexity Analysis

MethodTime ComplexitySpace Complexity
RecursiveO(logn)O(\log n)O(logn)O(\log n) (call stack)
IterativeO(logn)O(\log n)O(1)O(1)

Practice

LeetCode

References

  1. Python Official Documentation

  2. C++ Reference

  3. JavaScript MDN

  4. C++ Standard Template Library

  5. CS50x Week 3: Algorithms. (2025). Harvard University. Retrieved from https://cs50.harvard.edu/x/2025/weeks/3/

  1. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C.
    Introduction to Algorithms, 3rd/4th Edition, MIT Press.


💡

Some of my latest posts on LinkedIn

Last updated on