0% found this document useful (0 votes)
5 views3 pages

Binary Search Explanation

Binary Search is an efficient algorithm for finding an element in a sorted array with a time complexity of O(log n). The process involves repeatedly halving the search range by checking the middle element and adjusting the search bounds accordingly. A step-by-step example demonstrates how to implement the algorithm in Java and highlights the importance of preventing integer overflow when calculating the midpoint.

Uploaded by

dinesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Binary Search Explanation

Binary Search is an efficient algorithm for finding an element in a sorted array with a time complexity of O(log n). The process involves repeatedly halving the search range by checking the middle element and adjusting the search bounds accordingly. A step-by-step example demonstrates how to implement the algorithm in Java and highlights the importance of preventing integer overflow when calculating the midpoint.

Uploaded by

dinesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Binary Search Explanation

🔍 What is Binary Search?


Binary Search is an efficient algorithm to find an element in a sorted array by repeatedly
dividing the search range in half. It works only on sorted arrays and has a time complexity
of O(log n).

📝 Steps to Remember (HCM Rule - Half, Check, Move)


Half → Find the middle element.

Check → If it's the target, return the index.

Move →

- If target < mid, search left half

- If target > mid, search right half

- Repeat until left > right (element not found)

🔢 Example
Given Array (Sorted): {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}

Target: 23

🖥️ Java Code
int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1; // Not found
}
Dry Run (Step-by-Step Execution)

Initialization:

int left = 0; // First index (0)

int right = arr.length - 1; // Last index (9)

Step 1: Find Midpoint

mid = left + (right - left) / 2

= 0 + (9 - 0) / 2

= 4

arr[mid] = 16 (Element at index 4)


Since 23 < 16, move left to mid + 1
New Range: left = 5, right = 9

Step 2: Find New Midpoint

mid = 5 + (9 - 5) / 2

= 5 + 4 / 2

= 7

arr[mid] = 56 (Element at index 7)


Since 23 < 56, move right to mid - 1
New Range: left = 5, right = 6

Step 3: Find New Midpoint

mid = 5 + (6 - 5) / 2

= 5 + 1 / 2

= 5

arr[mid] = 23 (Element at index 5)


Target Found at Index 5!
📊 Dry Run (Step-by-Step Execution)
Iteration Left Right Mid arr[mid] Action
1 0 9 4 16 Move right
(left = 5)
2 5 9 7 56 Move left
(right = 6)
3 5 6 5 23 Found at
index 5

✅ Why mid = left + (right - left) / 2?


Prevents integer overflow (safe for large numbers).

More stable than mid = (left + right) / 2.

⚡ Final Thoughts
🏎 Fast → O(log n) time complexity

Efficient → Works great for large datasets

Key to Remember → 'Half, Check, Move'

You might also like