Binary search algorithm explained with c program.

Binary Search Algorithm Explained with C Program | Beginner Friendly Guide

Binary Search Algorithm Explained for Beginners

When I first learned searching algorithms, Linear Search seemed easy but slow. Then I discovered Binary Search, and it completely changed how I understood efficient searching.

Binary Search is one of the fastest searching algorithms used in computer science. In this article, I will explain it in a beginner-friendly way with animations, diagrams, examples, and a complete C program.

Binary Search works by repeatedly dividing the search space into half.

What is Binary Search?

Binary Search is a searching algorithm used to find an element in a sorted array. Instead of checking every element one by one, Binary Search checks the middle element and eliminates half of the remaining elements instantly.

Fast Searching = Less Comparisons + Dividing Array into Half

Real-Life Example of Binary Search

Imagine searching for a word in a dictionary. You do not start from page 1. Instead, you open the middle page and decide whether to search left or right.

That is exactly how Binary Search works.

Conditions for Binary Search

The array must be sorted before using Binary Search.

How Binary Search Works

Suppose we have the following sorted array:

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

We want to search for:

23

Step 1: Find the Middle Element

[2, 5, 8, 12, 16, 23, 38, 56, 72]

Middle Element = 16

Since 23 is greater than 16, we ignore the left half.

Step 2: Search Right Half

[23, 38, 56, 72]

Middle Element = 38

Since 23 is smaller than 38, we search the left side.

Step 3: Element Found

[23]

Element Found Successfully

Binary Search Visualization

Original Array:

[1, 3, 5, 7, 9, 11, 13]

Search for 9

Middle = 7
Move Right

Middle = 11
Move Left

Found 9

Binary Search Algorithm


while(low <= high)
{
    middle = (low + high) / 2

    if(element found)
        return index

    else if(target > middle)
        search right half

    else
        search left half
}
    

Binary Search Program in C


#include 

int binarySearch(int arr[], int size, int target)
{
    int low = 0;
    int high = size - 1;

    while(low <= high)
    {
        int mid = (low + high) / 2;

        if(arr[mid] == target)
        {
            return mid;
        }

        else if(arr[mid] < target)
        {
            low = mid + 1;
        }

        else
        {
            high = mid - 1;
        }
    }

    return -1;
}

int main()
{
    int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72};

    int size = sizeof(arr) / sizeof(arr[0]);

    int target = 23;

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

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

    return 0;
}
    

Output


Element found at index 5
    

Time Complexity of Binary Search

Case Complexity
Best Case O(1)
Average Case O(log n)
Worst Case O(log n)

Advantages of Binary Search

  • Very fast searching algorithm
  • Efficient for large datasets
  • Requires fewer comparisons

Disadvantages of Binary Search

  • Works only on sorted arrays
  • Sorting may take additional time

Binary Search vs Linear Search

Feature Binary Search Linear Search
Speed Fast Slow
Complexity O(log n) O(n)
Requires Sorted Array Yes No

Video Tutorial

Frequently Asked Questions

Why is Binary Search faster than Linear Search?

Binary Search cuts the search area in half after every comparison, making it much faster.

Can Binary Search work on unsorted arrays?

No. Binary Search only works correctly on sorted arrays.

Is Binary Search important for interviews?

Yes. Binary Search is one of the most common interview algorithms.

Final Thoughts

Binary Search is one of the most efficient searching algorithms every programmer should learn. Understanding it will help you improve your problem-solving and algorithm skills.

Try implementing the algorithm yourself and test it with different arrays to fully understand how it works.

Author Image

About the Author

Written by the team at Bsccoding.online to help beginner programmers learn programming concepts in a simple and practical way.

We create beginner-friendly tutorials on algorithms, programming, web development, and coding concepts.

Website: Bsccoding.online

Post a Comment

Post a Comment (0)

Previous Post Next Post
Update cookies preferences