Binary Search in C – Beginner-Friendly Tutorial with Example

Binary Search in C is one of the most important searching algorithms every beginner programmer must learn. It is widely used in competitive programming, technical interviews, and real-world software applications because of its speed and efficiency.

Binary Search in C

In this tutorial, you will learn Binary Search in C from scratch, even if you are completely new to programming.


What is Binary Search in C?

Binary Search is an efficient algorithm used to search an element in a sorted array.

Instead of checking each element one by one (like Linear Search), Binary Search:

  • Divides the array into two halves
  • Compares the middle element with the target value
  • Eliminates half of the array in each step

This makes Binary Search extremely fast.


Important Condition for Binary Search

⚠️ Binary Search works only on a sorted array

Example of a sorted array:

2, 5, 8, 12, 16, 23, 38

Example of an unsorted array (Binary Search will not work):

8, 2, 16, 5, 23, 12

Why Use Binary Search Instead of Linear Search?

Search MethodApproachSpeed
Linear SearchChecks every elementSlow
Binary SearchDivides array into halvesVery Fast

Binary Search is ideal when working with large datasets.


How Binary Search Works (Step-by-Step)

how binary search works

Consider this sorted array:

Index:  0   1   2   3   4   5   6
Array:  3   7   12  18  25  31  40

We want to search for 18.

Step 1: Find the middle element

mid = (start + end) / 2
mid = (0 + 6) / 2 = 3

Middle element = 18

Step 2: Compare

  • If middle element equals the key → Element found
  • If key is smaller → search left half
  • If key is larger → search right half

Since 18 == 18, the search stops successfully.


Binary Search Algorithm in C (Simple Steps)

  1. Initialize start = 0
  2. Initialize end = size - 1
  3. Repeat while start <= end
  4. Calculate middle index
  5. Compare middle element with key
  6. Move left or right accordingly

Binary Search Program in C

#include <stdio.h>

int binarySearch(int arr[], int size, int key) {
    int start = 0;
    int end = size - 1;

    while (start <= end) {
        int mid = (start + end) / 2;

        if (arr[mid] == key)
            return mid;

        else if (key < arr[mid])
            end = mid - 1;

        else
            start = mid + 1;
    }

    return -1;
}

int main() {
    int arr[] = {5, 10, 15, 20, 25, 30};
    int size = 6;
    int key = 20;

    int result = binarySearch(arr, size, key);

    if (result != -1)
        printf("Element found at index %d", result);
    else
        printf("Element not found");

    return 0;
}

Explanation of Binary Search Code in C

  • start → first index of the array
  • end → last index of the array
  • mid → middle position
  • If arr[mid] == key → element found
  • If key < arr[mid] → search left half
  • If key > arr[mid] → search right half

The loop continues until the element is found or the search space becomes empty.


Output

Element found at index 3

Time Complexity of Binary Search

CaseTime Complexity
Best CaseO(1)
Average CaseO(log n)
Worst CaseO(log n)

👉 O(log n) means the algorithm is extremely fast, even for large arrays.


Advantages of Binary Search

  • Faster than Linear Search
  • Efficient for large datasets
  • Reduces search space by half every step
  • Commonly asked in interviews

Disadvantages of Binary Search

  • Works only on sorted arrays
  • Not suitable for frequently changing data
  • Requires random access to elements

When to Use Binary Search?

✅ When data is sorted
✅ When performance is critical
✅ When working with large datasets


Frequently Asked Questions (FAQs)

Is Binary Search faster than Linear Search?

Yes, Binary Search is much faster, especially for large arrays.

Can Binary Search be used on unsorted arrays?

No, the array must be sorted first.

Is Binary Search recursive or iterative?

Binary Search can be implemented using both methods.


Conclusion

Binary Search in C is a fundamental algorithm that every beginner should master. It improves problem-solving skills and introduces the powerful divide-and-conquer technique.

Once you understand Binary Search, learning advanced algorithms becomes much easier.

See Also

Leave a Comment