0% found this document useful (0 votes)
2 views

Binary Search Math

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary Search Math

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question

BinarySearch(list[], min, max, key)

while min ≤ max do

mid = (max+min) / 2
if list[mid] >key then

max = mid-1

else if list[mid] <key then

min = mid+1

else

return mid

end if

end while
return false

Ans:

Understanding Binary Search

Binary Search is an efficient algorithm for finding a specific value within a sorted array. It works

by repeatedly dividing the search interval in half. Here's a breakdown of the provided code:

Function Parameters:

● list[]: The sorted array to be searched.


● min: The starting index of the search interval.

● max: The ending index of the search interval.

● key: The value to be searched for.

Algorithm Steps:

1. Initialize the search interval:

○ Set min to the first index of the array.

○ Set max to the last index of the array.


2. Iterate while the search interval is valid:
○ Calculate the middle index mid using integer division: mid = (min + max) / 2.

○ Compare list[mid] with the key:

■ If list[mid] is greater than key, the target element must lie in the left half of the

interval. So, update max to mid - 1.


■ If list[mid] is less than key, the target element must lie in the right half of the

interval. So, update min to mid + 1.

■ If list[mid] is equal to key, the target element is found, and the function returns

mid.

3. If the loop terminates without finding the key:

○ The function returns false, indicating that the key is not present in the array.

Key Points:

● Efficiency: Binary Search has a time complexity of O(log n), making it significantly faster
than linear search for large arrays.

● Precondition: The array must be sorted for Binary Search to work correctly.

● Integer Division: Ensure that mid is calculated using integer division to avoid potential

floating-point issues.

Example:

Consider the sorted array: [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]

To search for the key 12:


1. Initial Interval: min = 0, max = 9.

2. First Iteration:

○ mid = (0 + 9) / 2 = 4

○ list[mid] = 16 > key, so max becomes 3.

3. Second Iteration:

○ mid = (0 + 3) / 2 = 1

○ list[mid] = 5 < key, so min becomes 2.


4. Third Iteration:
○ mid = (2 + 3) / 2 = 2

○ list[mid] = 8 < key, so min becomes 3.

5. Fourth Iteration:

○ mid = (3 + 3) / 2 = 3
○ list[mid] = 12 = key, so the function returns 3.

Implementation in Different Programming Languages:

You can implement Binary Search in various programming languages, such as Python, Java,

C++, etc. The core logic remains the same, but the syntax and specific implementation details
may vary.
Additional Considerations:

● Recursive Implementation: Binary Search can also be implemented recursively.

● Iterative Implementation: The provided code is an iterative implementation, which is

often more efficient in terms of memory usage.

● Edge Cases: Consider edge cases like empty arrays or arrays with only one element.
By understanding these concepts and the implementation details, you can effectively use Binary

Search to efficiently search sorted arrays.

You might also like