LibraryBinary Search

Binary Search

Learn about Binary Search as part of GATE Computer Science - Algorithms and Data Structures

Binary Search: Efficiently Finding Your Way in Sorted Data

Binary search is a highly efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

The Core Idea: Divide and Conquer

Binary search eliminates half of the remaining search space with each comparison.

Imagine looking for a word in a dictionary. You don't start from the first page; you open it roughly in the middle. If the word you're looking for comes alphabetically after the page you opened, you know it's in the second half. If it comes before, it's in the first half. You repeat this process, narrowing down your search.

The algorithm starts by comparing the target value with the middle element of the sorted array. If the target value matches the middle element, its position is returned. If the target value is less than the middle element, the search continues in the lower half of the array. If the target value is greater than the middle element, the search continues in the upper half of the array. This process is repeated until the target value is found or the search interval is empty.

What is the fundamental prerequisite for binary search to work correctly?

The list or array must be sorted.

How Binary Search Works: Step-by-Step

Loading diagram...

The process involves maintaining a search space defined by a 'low' and 'high' index. The middle index is calculated as

code
mid = floor((low + high) / 2)
. The target is compared with the element at
code
arr[mid]
. If
code
arr[mid]
equals the target, we've found it. If the target is smaller, we update
code
high = mid - 1
. If the target is larger, we update
code
low = mid + 1
. This continues until
code
low > high
, indicating the element is not present.

Time Complexity: The Power of Logarithms

Binary search exhibits a logarithmic time complexity, denoted as O(log n). This means that as the size of the input list (n) doubles, the number of comparisons required to find an element only increases by one. This efficiency stems from the algorithm's ability to discard half of the search space in each step. For instance, searching in a list of 1024 elements takes at most 10 comparisons (2^10 = 1024), whereas a linear search could take up to 1024 comparisons in the worst case.

📚

Text-based content

Library pages focus on text content

The O(log n) complexity makes binary search incredibly fast for large datasets, a crucial advantage in competitive programming and real-world applications.

Variations and Considerations

There are two main implementations: iterative and recursive. The iterative approach uses a loop, while the recursive approach calls itself with updated search bounds. Both achieve the same time complexity. Edge cases, such as an empty array or a target value outside the array's range, must be handled correctly. Understanding integer overflow when calculating

code
mid
(e.g.,
code
mid = low + (high - low) / 2
) is also important for robust implementations.

FeatureBinary SearchLinear Search
PrerequisiteSorted DataUnsorted Data
Time Complexity (Best)O(1)O(1)
Time Complexity (Average)O(log n)O(n)
Time Complexity (Worst)O(log n)O(n)
Efficiency for Large DataVery HighLow

Binary Search in Competitive Exams (GATE CS)

Binary search is a fundamental algorithm tested in GATE CS. Questions often involve finding elements, determining the number of occurrences, or applying binary search on modified data structures or problem constraints. Understanding its implementation details, time complexity, and how to adapt it to specific problem scenarios is key to scoring well.

Learning Resources

Binary Search - GeeksforGeeks(documentation)

A comprehensive explanation of binary search with C++, Java, and Python implementations, including iterative and recursive approaches.

Binary Search Algorithm - Tutorialspoint(tutorial)

Provides a clear explanation of the binary search algorithm, its working principle, and its time complexity.

Binary Search - Wikipedia(wikipedia)

An in-depth overview of binary search, its history, variations, and applications.

Binary Search - Programiz(tutorial)

Learn how binary search works with a step-by-step example and code implementations in Python.

Binary Search Explained (with animation)(video)

A visual explanation of the binary search algorithm, making it easier to grasp the divide-and-conquer approach.

Binary Search - CS Stack Exchange(blog)

A community discussion on the fundamentals and nuances of binary search, offering different perspectives.

Binary Search - HackerRank(tutorial)

Explore binary search concepts and practice problems on HackerRank, a popular platform for coding challenges.

Algorithms Specialization - Stanford University (Coursera)(tutorial)

This specialization covers fundamental algorithms, including binary search, in detail, with practical applications.

Binary Search - Codecademy(tutorial)

An interactive tutorial to understand and implement binary search, suitable for beginners.

Binary Search Tree vs Binary Search Algorithm(documentation)

Clarifies the distinction between the binary search algorithm and a binary search tree data structure.