C program for Binary search algorithm

# C program for Binary search algorithm

4024
6
SHARE

C program for binary search algorithm : Binary search is a searching algorithm which searches an element in the sorted list. In each step,binary search algorithm compares the input number(to be searched) with the value of the middle element of the array. If the values match, then a matching element has been found so its index, or position, is returned. Otherwise, if the searched number is less than the middle element’s value, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input number is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the number cannot be found in the array then a special “Not found” indication is returned. Let us see a basic implementation of binary search algorithm using iterations.

```int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
/* calculate the midpoint for roughly equal partition */
int imid = midpoint(imin, imax);

// determine which subarray to search
if      (A[imid] <  key)
// change min index to search upper subarray
imin = imid + 1;
else if (A[imid] > key )
// change max index to search lower subarray
imax = imid - 1;
else
// key found at index imid
return imid;
}
return KEY_NOT_FOUND;
}```

The above code is just an snippet to see how the binary search algorithm works. Now lets see the complete algorithm.

Note : The input list should be in sorted order (ascending order) else program will not work as expected. If you want to provide the unsorted list of elements as input then first of all you need to sort the elements and then do the binary search.

Have a look at different sorting programs:

C program of Bubble Sort Algorithm

C program for Selection Sort Algorithm

C program of Merge Sort Algorithm

Now lets see complete C code of binary search algorithm.

### C program for Binary search algorithm using iterations :

```#include<stdio.h>
#include<conio.h>
main()
{
int c, first, last, middle, n, search, array[100];
printf("***************************************************\n");
printf("**************written by Ccodechamp****************\n");
printf("***************************************************\n\n\n");
printf("Enter number of elements: ");
scanf("%d",&n);

printf("\nEnter %d integers in sorted sorted :\n", n);

for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);

printf("\nEnter value to be searched in above list : ");
scanf("%d",&search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("\n%d found at position %d.\n", search, middle+1);
getch();
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if ( first > last )
getch();

return 0;
}```

We hope that you all like the above . If you have any queries regarding the logic ask us in form of comments.

SHARE
Previous articleC program to reverse a number | C code champ
Next articleC program to find armstrong number
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com