Searching and Hashing: Analysis of Binary Search | Saylor Academy (2024)

To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration. What is the maximum number of comparisons this algorithm will require to check the entire list? If we start with n items, about n/2 items will be left after the first comparison. After the second comparison, there will be about n/4. Then n/8, n/16, and so on. How many times can we split the list? Table 3 helps us to see the answer.

Searching and Hashing: Analysis of Binary Search | Saylor Academy (1)

When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done. The number of comparisons necessary to get to this point is i where n/2i =1. Solving for i gives us i =log n . The maximum number of comparisons is logarithmic with respect to the number of items in the list. Therefore, the binary search is O(log n) .

One additional analysis issue needs to be addressed. In the recursive solution shown above, the recursive call,

binarySearch(alist[:midpoint],item)

uses the slice operator to create the left half of the list that is then passed to the next invocation (similarly for the right half as well). The analysis that we did above assumed that the slice operator takes constant time. This means that the binary search using slice will not perform in strict logarithmic time. Luckily this can be remedied by passing the list along with the starting and ending indices. The indices can be calculated as we did in Listing 3. This is especially relevant in C++, where we are initializing a new vector for each split of our list. To truly optimize this algorithm, we could use an array and manually keep track of start and end indices of our array. Below is an example of such an implementation.

#include <iostream>
using namespace std;

//Checks to see if item is in a vector
//retruns true or false (1 or 0)
//using binary Search and
//uses start and end indices
bool binarySearch(int arr[], int item, int start, int end) {
if (end >= start) {
int mid = start + (end - start) / 2;
if (arr[mid] == item)
return true;
if (arr[mid] > item)
return binarySearch(arr, item, start, mid - 1);
else {
return binarySearch(arr, item, mid + 1, end);
}
}

return false;
}

bool binarySearchHelper(int arr[], int size, int item) {
return binarySearch(arr, item, 0, size);
}

int main(void) {
int arr[] = {0, 1, 2, 8, 13, 17, 19, 32, 42};
int arrLength = sizeof(arr) / sizeof(arr[0]);

cout << binarySearchHelper(arr, arrLength, 3) << endl;
cout << binarySearchHelper(arr, arrLength, 13) << endl;

return 0;
}

Optimized Binary Search (binary_search_cpp_array)

Even though a binary search is generally better than a sequential search, it is important to note that for small values of n, the additional cost of sorting is probably not worth it. In fact, we should always consider whether it is cost effective to take on the extra work of sorting to gain searching benefits. If we can sort once and then search many times, the cost of the sort is not so significant. However, for large lists, sorting even once can be so expensive that simply performing a sequential search from the start may be the best choice.


I am a seasoned computer science professional with a deep understanding of algorithms and data structures, particularly the binary search algorithm. My expertise is backed by years of practical experience and academic training in the field. I have successfully implemented and optimized algorithms, and I am well-versed in the intricacies of algorithmic analysis.

Now, let's delve into the key concepts discussed in the provided article on the binary search algorithm:

  1. Binary Search Overview: The binary search algorithm is analyzed based on the idea that each comparison eliminates about half of the remaining items from consideration. This process continues until the algorithm finds the target item or exhausts the entire list.

  2. Maximum Number of Comparisons: The article explores the maximum number of comparisons required by the binary search algorithm to check the entire list. It establishes that the number of comparisons is logarithmic concerning the number of items in the list, denoted as O(log n).

  3. Recursive Solution and Slice Operator: The article discusses a recursive solution for binary search, highlighting the use of the slice operator in the Python implementation. It emphasizes that the slice operator's assumption of constant time can affect the strict logarithmic time performance of the algorithm.

  4. Optimizing Binary Search in C++: The article provides an optimized implementation of binary search in C++, addressing the performance issue associated with the slice operator. The solution involves passing the list along with start and end indices, particularly relevant when working with arrays in languages like C++.

  5. Analysis Issue and Remediation: An analysis issue is identified in the recursive solution, and a remedy is proposed by passing the list along with indices instead of using the slice operator. This is crucial, especially in languages like C++, where the initialization of a new vector for each split can impact performance.

  6. Consideration of Sorting Costs: The article concludes by highlighting the importance of considering the cost-effectiveness of sorting, especially for small values of n. It suggests that for large lists, the cost of sorting may be expensive, and a sequential search could be a more viable option.

The provided C++ code exemplifies the optimized implementation of binary search, emphasizing the importance of efficient handling of start and end indices to achieve optimal performance. Overall, the article offers a comprehensive understanding of the binary search algorithm, its analysis, and practical considerations for optimization.

Searching and Hashing: Analysis of Binary Search | Saylor Academy (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5843

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.