C Programming

How to Implement Binary Search in C

Binary search is a searching technique used to allocate the exact position of a required element in a sorted array. It divides the array into two parts repeatedly from interval until it finds the exact element in an array. Binary search is sometimes referred to as divide and conquer algorithm because it divides the array into multiple pieces and performs the search until the element is found. Binary search is a fast and simple searching method for finding the element at a specific position in quick time.

In this article, we will show you how to implement binary search in the C programming language.

How to Implement Binary Search in C

Developers use binary search to simplify the searching process since it is quite beneficial in providing you with the results in a very short amount of time. The time complexity of the binary search algorithm is O(logN), which can be effective in a program where the given dataset is too large to be searched linearly.

The Algorithm of Binary search in C works in the following way:

  • Firstly, you define the pivot element which you want to search.
  • If pivot value=center value then search is completed else continue.
  • Compare the pivot element with the center element in the array.
  • If the pivot value is < than the center element, it will search the element from left side of array to center element.
  • If the pivot value is > than the center element value then it will search from the right side of the array.
  • Repeat the last two steps until you get the pivot.

Following is the implementation of Binary search program in C language:

#include <stdio.h>
int main ()
{
int i, left, right, middle, num, pivot, newarr[50];
printf("Please Enter the total number of Element:");
scanf("%d",&num);
printf("Enter %d integer element: ", num);
for(i = 0; i < num; i++)
scanf("%d",&newarr[i]);
printf("Please Enter the Value You can find: ");
scanf("%d", &pivot);
left = 0;
right = num - 1;
middle = (left+right)/2;
while (left <= right) {
if(newarr[middle] < pivot)
left = middle + 1;
else if (newarr[middle] == pivot) {
printf("%d found at location %d.num", pivot, middle+1);
break;
}
else
right = middle - 1;
middle = (left + right)/2;
}
if(left > right)
printf("The element is not found! %d it is not present in the list.num", pivot);
return 0;
}

In the above code, we first initialize the variables, then take the total number of elements from the user by num variable and take values in the array from the user until i<num. Then from the pivot variable, we decide the value to match and matching start from left index 0 to end index. We then divide the array as middle=(left+right)/2. After this, we use the while loop to find the pivot through the if else condition that finds the element and generate an output with the element index number if found otherwise it will throw an element not found error.

Here is the output of the code.

Conclusion

Binary search is a powerful algorithm for narrowing down a selection of items in an array. It divides the section of the list into halves that could really contain the object in half and repeat the process again till there’s just one feasible position or result left. In the above-mentioned guidelines, we have seen what binary search is; and how we can use binary search in C language code. In short, binary search is a very useful searching technique in C language.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.