Photo AI

Last Updated Sep 27, 2025

Binary Search Simplified Revision Notes

Revision notes with simplified explanations to understand Binary Search quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

248+ students studying

Binary Search

Overview

Binary Search is an efficient algorithm used to find the position of a target value within a sorted dataset. It works by repeatedly dividing the search interval in half, and comparing the middle element of the interval with the target value. Binary Search is much faster than Linear Search for large datasets.

How Binary Search Works

  • Pre-condition: The data must be sorted.
  • Process:
    1. Identify the middle element of the dataset.
    2. Compare the middle element with the target value:
    • If it matches, the target is found.
    • If the target is smaller, restrict the search to the left half.
    • If the target is larger, restrict the search to the right half.
    1. Repeat the process until the target is found or the interval is empty.

Time Complexity

  • Best Case: O(1) (target is the middle element).
  • Worst Case: O(log n) (halves the search space with each step).
  • Space Complexity:
    • O(1) for iterative implementation.
    • O(log n) for recursive implementation due to call stack.
infoNote

Example: Binary Search

Given a sorted array:

2, 4, 7, 10, 15, 18, 20, 25, 30

Target: 18

Step-by-Step Execution:

  1. Initial array: 2, 4, 7, 10, 15, 18, 20, 25, 30 Middle element = 15 (index 4).

Since 18 > 15, search the right half: 18, 20, 25, 30

  1. New array: 18, 20, 25, 30 Middle element = 20 (index 6).

Since 18 < 20, search the left half: 18

  1. New array: 18 Middle element = 18 (index 5).

Match found.

Position: Index 5.

Pseudocode for Binary Search

Iterative Approach:

METHOD BinarySearch(array, target)
    low ← 0
    high ← array.length - 1

    WHILE low <= high
        mid ← (low + high) DIV 2
        IF array[mid] = target
            RETURN mid
        ELSE IF array[mid] < target
            low ← mid + 1
        ELSE
            high ← mid - 1
    ENDWHILE

    RETURN -1  // Target not found

Recursive Approach:

METHOD BinarySearchRecursive(array, target, low, high)
    IF low > high
        RETURN -1  // Target not found

    mid ← (low + high) DIV 2
    IF array[mid] = target
        RETURN mid
    ELSE IF array[mid] < target
        RETURN BinarySearchRecursive(array, target, mid + 1, high)
    ELSE
        RETURN BinarySearchRecursive(array, target, low, mid - 1)

Python Example

Iterative Implementation:

def binary_search(array, target):
    low = 0
    high = len(array) - 1

    while low <= high:
        mid = (low + high) // 2
        if array[mid] == target:
            return mid
        elif array[mid] < target:
            low = mid + 1
        else:
            high = mid - 1

    return -1  # Target not found

# Example usage:
data = [2, 4, 7, 10, 15, 18, 20, 25, 30]
print(binary_search(data, 18))  # Output: 5

Recursive Implementation:

def binary_search_recursive(array, target, low, high):
    if low > high:
        return -1  # Target not found

    mid = (low + high) // 2
    if array[mid] == target:
        return mid
    elif array[mid] < target:
        return binary_search_recursive(array, target, mid + 1, high)
    else:
        return binary_search_recursive(array, target, low, mid - 1)

# Example usage:
data = [2, 4, 7, 10, 15, 18, 20, 25, 30]
print(binary_search_recursive(data, 18, 0, len(data) - 1))  # Output: 5

Tracing Binary Search

infoNote

Example:

Given sorted array: 3, 8, 11, 14, 18, 25, 30, 36

Target: 25

  1. Initial range: Low = 0, High = 7, Mid = (0 + 7) // 2 = 3
  • Compare array[3] = 14.
  • 25 > 14 → Search right half.
  1. New range: Low = 4, High = 7, Mid = (4 + 7) // 2 = 5
  • Compare array[5] = 25.
  • Match found at index 5.

Note Summary

infoNote

Common Mistakes

  1. Not Handling Edge Cases:
  • Forgetting to check for an empty array.
  • Incorrectly updating low and high when the target is not found.
  1. Infinite Loops:
  • Miscalculating mid or improperly updating low and high can result in infinite loops.
  1. Incorrect Base Condition in Recursion:
  • Failing to return when low > high.
  1. Unsorted Input:
  • Binary Search only works on sorted data. Applying it to unsorted data leads to incorrect results.
infoNote

Key Takeaways

  • Binary Search efficiently finds a target in a sorted dataset, reducing the search space by half with each iteration.
  • It has a time complexity of O(log n), making it much faster than Linear Search for large datasets.
  • Both iterative and recursive implementations are widely used.
  • Ensure the dataset is sorted and maintain correct index bounds (low, high) for accurate results.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Binary Search

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Binary Search

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Binary Search

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Binary Search

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Binary Search

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Binary Search

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Binary Search you should explore

Discover More Revision Notes Related to Binary Search to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

295+ studying

190KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

236+ studying

195KViews

96%

114 rated

Algorithms for the Main Data Structures

Trees

user avatar
user avatar
user avatar
user avatar
user avatar

340+ studying

197KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

231+ studying

181KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered