Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Binary Search quickly and effectively.
248+ students studying
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.
Given a sorted array:
2, 4, 7, 10, 15, 18, 20, 25, 30
Target: 18
Step-by-Step Execution:
Since 18 > 15, search the right half: 18, 20, 25, 30
Since 18 < 20, search the left half: 18
Match found.
Position: Index 5.
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
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)
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
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
Given sorted array: 3, 8, 11, 14, 18, 25, 30, 36
Target: 25
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 Flashcards12 quizzes
Quizzes on Binary Search
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Binary Search
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Binary Search
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Binary Search
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Binary Search to Deepen Your Understanding and Improve Your Mastery
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!
Report Improved Results
Recommend to friends
Students Supported
Questions answered