C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Binary Search Program

C Program: Binary Search Program

Method 1: Binary Search (Iterative Method)

C

#include <stdio.h>

 

int main() {

    int arr[100], n, key, low, high, mid, found = 0, i;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements (must be sorted)

    printf("Enter %d sorted elements (ascending order):\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Input element to search

    printf("Enter element to search: ");

    scanf("%d", &key);

 

    // Initialize search boundaries

    low = 0;

    high = n - 1;

 

    // Binary search using loop

    while(low <= high) {

        mid = (low + high) / 2;

 

        if(arr[mid] == key) {

            printf("\nElement %d found at position %d\n", key, mid + 1);

            found = 1;

            break;

        }

        else if(arr[mid] < key)

            low = mid + 1;

        else

            high = mid - 1;

    }

 

    if(!found)

        printf("\nElement %d not found in the array\n", key);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 6
Enter 6 sorted elements:
10 20 30 40 50 60
Enter element to search: 40

OUTPUT :
Element 40 found at position 4

Explanation

  1. Binary Search works only on sorted arrays.
  2. The algorithm divides the search range into halves:
    • Calculate mid = (low + high) / 2.
    • If arr[mid] == key → element found.
    • If arr[mid] < key → search right half.
    • Else → search left half.
  3. This process continues until low > high or the element is found.

 

C Program: Binary Search Program

Method 2: Binary Search (Recursive Method)

C

#include <stdio.h>

 

// Recursive binary search function

int binarySearch(int arr[], int low, int high, int key) {

    if (low > high)

        return -1; // Element not found

 

    int mid = (low + high) / 2;

 

    if (arr[mid] == key)

        return mid;

    else if (arr[mid] > key)

        return binarySearch(arr, low, mid - 1, key);

    else

        return binarySearch(arr, mid + 1, high, key);

}

 

int main() {

    int arr[100], n, key, i, result;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input sorted array elements

    printf("Enter %d sorted elements (ascending order):\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Input search key

    printf("Enter element to search: ");

    scanf("%d", &key);

 

    // Call recursive function

    result = binarySearch(arr, 0, n - 1, key);

 

    if(result != -1)

        printf("\nElement %d found at position %d\n", key, result + 1);

    else

        printf("\nElement %d not found in the array\n", key);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 7
Enter 7 sorted elements:
2 4 6 8 10 12 14
Enter element to search: 10

OUTPUT :
Element 10 found at position 5

Explanation

  • The function calls itself recursively:
    • Base case → when low > high, element not found.
    • Recursive step → narrow down the search range.
  • Much shorter code and elegant logic.