# Binary Search

April 28, 2023

Binary search is a widely used search algorithm that is used to search for a specific element in a sorted list of elements. It is an efficient algorithm with a time complexity of O(log n), where n is the number of elements in the list. This algorithm works by repeatedly dividing the search interval in half, eliminating half of the remaining elements each time, until the target element is found, or the search interval is empty.

The purpose of the binary search algorithm is to search a sorted list of elements for a specific value efficiently. It is useful when searching for a specific element in a large list of elements as it reduces the number of elements that need to be searched. Binary search is widely used in computer science and is an essential algorithm in programming and software development.

## Brief History and Development

The binary search algorithm was first introduced by John von Neumann, a mathematician, and computer scientist in 1945. It was later popularized by Donald Knuth in his book, “The Art of Computer Programming.” Since then, it has become one of the most commonly used algorithms in computer science.

## Key Concepts and Principles

The binary search algorithm works by dividing the search interval in half repeatedly, eliminating half of the remaining elements each time until the target element is found. The algorithm requires that the list be sorted in ascending or descending order. The key concepts and principles of the binary search algorithm include:

### 1. Divide and Conquer

The binary search algorithm follows the divide and conquer approach, where the problem is divided into smaller subproblems and solved independently. In this case, the search interval is divided into smaller intervals until the target value is found.

### 2. Sorted List

Binary search requires that the list be sorted in ascending or descending order. This is because the algorithm compares the target value with the middle element of the list and eliminates half of the remaining elements based on whether the target value is greater or less than the middle element. If the list is not sorted, the algorithm will not work correctly.

### 3. Time Complexity

Binary search has a time complexity of O(log n) where n is the number of elements in the list. This is because the algorithm divides the search interval in half each time, reducing the number of elements that need to be searched.

## Pseudocode and Implementation Details

The following is the pseudocode for the binary search algorithm:

```
binarySearch(list, target):
low = 0
high = length(list) - 1
while low <= high:
mid = (low + high) / 2
if list[mid] == target:
return mid
elif list[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
```

The binary search algorithm takes two arguments: the list to be searched and the target value. The algorithm initializes a low and high variable to the first and last indices of the list, respectively. It then enters a loop that continues until the low index is greater than the high index. Within the loop, the algorithm calculates the mid index by adding the low and high indices and dividing by two. It then checks if the middle element of the list is equal to the target value. If it is, the algorithm returns the index of the middle element. If the middle element is less than the target value, the algorithm sets the low index to the mid index plus one. If the middle element is greater than the target value, the algorithm sets the high index to the mid index minus one. If the target value is not found, the algorithm returns -1.

## Examples and Use Cases

Binary search is a powerful algorithm used in various applications such as:

- Phonebook search – finding a phone number in a phonebook
- Dictionary search – searching for a word in a dictionary
- Searching in a database – finding a record in a database
- Binary search trees – a data structure that uses binary search to store and retrieve data in a tree-like structure

For example, let’s say we have a list of numbers [1, 2, 3, 4, 5, 6, 7, 8, 9]. We want to search for the number 5 in the list using binary search. The algorithm would start by initializing the low and high indices to 0 and 8, respectively. It would then calculate the mid index as 4, which is the middle element in the list. Since the middle element is less than the target value, the algorithm would set the low index to 5 and recalculate the mid index as 6. It would then compare the middle element to the target value and return the index if they match.

## Advantages and Disadvantages

Binary search has several advantages and disadvantages, including:

### Advantages

**Efficiency**– binary search has a time complexity of O(log n), making it an efficient algorithm for searching large lists of elements.**Versatility**– binary search can be used to search for any value in a sorted list of elements, making it a versatile algorithm.**Accuracy**– binary search is an accurate algorithm that always returns the correct index of the target value if it exists in the list.

### Disadvantages

**Sorted List**– binary search requires that the list be sorted in ascending or descending order, which can be a disadvantage if the list is not sorted.**Additional Memory**– binary search requires additional memory to store the low and high indices and the middle element, which can be a disadvantage in memory-limited systems.

## Related Algorithms or Variations

There are several related algorithms or variations of binary search, including:

### 1. Interpolation Search

Interpolation search is a variation of binary search that uses a linear interpolation to calculate the mid index, rather than dividing the search interval in half. This can result in faster search times for lists that have a non-uniform distribution of elements.

### 2. Exponential Search

Exponential search is another variation of binary search that uses an exponential search algorithm to find an appropriate search interval, followed by binary search to locate the target value. This can result in faster search times for large lists of elements.

### 3. Ternary Search

Ternary search is a search algorithm that divides the search interval into three parts instead of two, reducing the number of comparisons required to find the target value. However, it has a time complexity of O(log3 n), which is slower than binary search.