Featured Article

Binary Search Algorithm in C: Step-by-Step Example

William Aug 14, 2026

The Binary Search Algorithm, a foundation in computer science, is a highly efficient search algorithm used to locate an item in a sorted array. It functions by repeatedly dividing the search interval in half until the target is found or the interval is empty. Let's dive into the intricacies of this powerful algorithm, accompanied by an illustrative example in C.

What is binary search algorithm?  -GeeksforGeeks
What is binary search algorithm? -GeeksforGeeks

Binary search's efficiency lies in its logarithmic time complexity, beating linear search in case of large data sets. It's implemented best when the data is sorted and when the dataset's size is substantial. However, it's important to note that binary search only works with sorted data, which might necessitate a sorting algorithm beforehand.

binary search algorithm
binary search algorithm

Understanding the Binary Search Process

Binary search works on the divide and conquer principle. At every step, it compares the target value with the middle element of the array. If the target matches the middle element, its position is returned. If the target is less than the middle element, the search continues in the lower half of the array. Otherwise, it continues in the upper half.

Searching Algorithms in C++ – Linear vs Binary Search
Searching Algorithms in C++ – Linear vs Binary Search

This process continues recursively until the target is found or half of the array's size becomes 1 (in which case the target isn't present in the array). The key here is that with each comparison, the search space is halved, ensuring quick convergence.

Binary Search Algorithm Pseudo-code

Simple Binary search code example in cpp  ~ C++ Programming Tutorial for Beginners
Simple Binary search code example in cpp ~ C++ Programming Tutorial for Beginners

To illustrate the process better, let's go through its pseudocode:

  • If the list is empty, return -1 (target not found).
  • Otherwise, calculate mid = (first + last) / 2.
  • If target is equal to mid, return mid.
  • Otherwise, if target is less than mid, recursively search the left half (first to mid - 1).
  • Otherwise, recursively search the right half (mid + 1 to last).

Binary Search in C with Example

Binary Search Explained Visually | DSA Guide for Beginners
Binary Search Explained Visually | DSA Guide for Beginners

Binary search can be easily implemented in C, owing to the language's versatility and low-level nature. Here's a simple demonstration:

```c #include int binary_search(int arr[], int first, int last, int target) { int mid; if (first <= last) { mid = (first + last) / 2; if (target == arr[mid]) return mid; else if (target < arr[mid]) return binary_search(arr, first, mid - 1, target); else return binary_search(arr, mid + 1, last, target); } return -1; // Target not found } int main() { int arr[] = {2, 3, 4, 10, 40}; int n = sizeof(arr)/sizeof(arr[0]); int target = 10; int result = binary_search(arr, 0, n - 1, target); if (result == -1) printf("Target Not Found\n"); else printf("Target Found at index %d\n", result); return 0; } ```

The provided C code embodies the binary search process, where the function `binary_search` utilizes recursion to efficiently find the target value in an array. The `main` function calls this search function to illustrate its usage.

Applications and Considerations

Linear And Binary Search Array in C - Probabu
Linear And Binary Search Array in C - Probabu

Binary search is widely used in various applications, leveraging its speed and utility. Some of its uses are in data files, database indexing, file systems, and language libraries.

However, it's important to keep in mind that binary search works on sorted data. Unsorted data first needs to be sorted using an appropriate sorting algorithm, which might have a time complexity of O(n log n) or worse, thus potentially negating binary search's time effectiveness.

Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Searching Algorithms Explained 🔍 | Linear vs Binary vs Jump vs Interpolation | DSA Made Easy 🚀
Searching Algorithms Explained 🔍 | Linear vs Binary vs Jump vs Interpolation | DSA Made Easy 🚀
Must Know Algorithms for Coding Interviews
Must Know Algorithms for Coding Interviews
How Binary Search Algorithm Works? Java Example without Recursion
How Binary Search Algorithm Works? Java Example without Recursion
Algorithm types and algorithm examples
Algorithm types and algorithm examples
How to Implement Binary Search Using Iterative Method
How to Implement Binary Search Using Iterative Method
Top 20 Algorithms Interview Problems for Programmers and Software Engineers
Top 20 Algorithms Interview Problems for Programmers and Software Engineers
Complete Computer Science & Cybersecurity Infographics Bundle
Complete Computer Science & Cybersecurity Infographics Bundle
30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners
30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners
How to code Binary Search Algorithm using Recursion in Java? Example
How to code Binary Search Algorithm using Recursion in Java? Example
💜 Linear Search vs Binary Search 🔍
💜 Linear Search vs Binary Search 🔍
Binary Search Explained Step-by-Step
Binary Search Explained Step-by-Step
Iterative and Recursive Binary Search Algorithm Implementation in Java
Iterative and Recursive Binary Search Algorithm Implementation in Java
Binary Search Flow Explained 🔍 | Step-by-Step DSA Guide ⚡ | Easy Example
Binary Search Flow Explained 🔍 | Step-by-Step DSA Guide ⚡ | Easy Example
binary search
binary search
Binary Search in Java - Scientech Easy
Binary Search in Java - Scientech Easy
a notebook with diagrams on how to use the search engine for an interactive learning tool
a notebook with diagrams on how to use the search engine for an interactive learning tool
Binary Image Representation – TeachWithICT
Binary Image Representation – TeachWithICT
How to implement Binary Search | Algorithm & Its Implementation | @TechRanch
How to implement Binary Search | Algorithm & Its Implementation | @TechRanch

Binary Search Variations

There are several variants to binary search to optimize its usage in different scenarios. Some popular variants include Interpolation Search (useful when the target is uniformly distributed), Exponential Search (efficient when the values range widely), and Fibonacci Search (useful for large arrays).

Understanding these variants can open up more efficient search possibilities, depending on the specific needs and nature of the data.

The binary search algorithm, with its precision and speed, plays a pivotal role in computer science. Whether you're a seasoned developer or a novice learning to code, understanding and being able to implement binary search is an essential step in your journey. This algorithm isn't merely a foundation – it's a powerful tool in your toolbox. So get practicing, and happy coding!