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

Binary Search Explained (1)

Uploaded by

soham waman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Binary Search Explained (1)

Uploaded by

soham waman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Binary Search Explained

Binary search is an efficient algorithm for finding the position of a target element
within a sorted list. It works by repeatedly dividing the search interval in half until the
target value is found or the interval is empty.

Key Points:

1. Precondition: The list must be sorted.

2. Efficiency: Binary search has a time complexity of , making it significantly faster


than linear search for large datasets.

Steps of Binary Search:

1. Initialize:

Start with two pointers: one pointing to the beginning of the list (low) and the other to
the end (high).

2. Divide:

Calculate the middle index:

Compare the target value with the middle element.

Conquer: If the middle element equals the target, the search is successful.

If the target is less than the middle element, adjust the high pointer to mid - 1
(narrowing the search to the left half).

If the target is greater than the middle element, adjust the low pointer to mid + 1
(narrowing the search to the right half).

1.
Repeat:

1.

Repeat the process on the narrowed interval until the target is found or low > high,
indicating the target is not in the list.

Example:

Consider a sorted list: .


You want to find the position of 7.

1. Initial pointers:

1.

low = 0, high = 6 (indices of the first and last elements).

First iteration:

1. The middle element is 7 (index 3).


2. Target found!

Java Implementation:
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;

while (low <= high) {


int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // Target found
} else if (arr[mid] < target) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}

return -1; // Target not found


}

public static void main(String[] args) {


int[] arr = {1, 3, 5, 7, 9, 11, 13};
int target = 7;
int result = binarySearch(arr, target);
System.out.println(result != -1 ? "Target found at index " +
result : "Target not found");
}
}

Example: Binary Search

1. Initial List: Say you have a sorted list of size . Binary search divides the list in half
at each step:

Step 1: Check the middle of 16 elements.

Step 2: Narrow to 8 elements.

Step 3: Narrow to 4 elements.

Step 4: Narrow to 2 elements.

Step 5: Narrow to 1 element.

After divisions, you reach the target or conclude it’s absent.

General Rule: In binary search, each iteration (repetition) reduces the problem size
by a factor of 2, so it completes in time.

5. Why is Binary search Good?

Algorithms with complexity are very efficient for large inputs. Even for a dataset of a
billion elements (), , meaning only about 30 iterations are needed.

Two Pseudo Code Examples of Binary Search

1. Iterative Binary Search

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

WHILE low ≤ high DO:


mid ← (low + high) // 2
IF array[mid] == target THEN:
RETURN mid // Target found
ELSE IF array[mid] < target THEN:
low ← mid + 1 // Search in the right half
ELSE:
high ← mid - 1 // Search in the left half

RETURN -1 // Target not found


2. Recursive Binary Search

BinarySearchRecursive(array, target, low, high):


IF low > high THEN:
RETURN -1 // Target not found

mid ← (low + high) // 2

IF array[mid] == target THEN:


RETURN mid // Target found
ELSE IF array[mid] < target THEN:
RETURN BinarySearchRecursive(array, target, mid + 1,
high) // Right half
ELSE:
RETURN BinarySearchRecursive(array, target, low, mid - 1) //
Left half

Real-World Analogy

Finding a Word in a Dictionary

Imagine you’re looking for the word "Binary" in a dictionary:

1. Open the dictionary at the middle:


2. Check the word on the middle page.
3. If it’s "Binary", you’re done.
4. If the word isn’t "Binary":
5. If the middle word comes before "Binary" alphabetically, ignore the left half and
search only in the right half.
6. If the middle word comes after "Binary", ignore the right half and search only in
the left half.
7. Repeat:

Keep splitting the remaining section of the dictionary in half until you find the word
or run out of pages.

STEPS TO CONSTRUCTING AN ALGORITHM


1. D
2. A
3. D
4. S
5. I
6. T

You might also like