# What is binary search? write a C program for binary search

(@thiyagarajan)
Eminent Member
Joined:2 years  ago
Posts: 26
27/01/2017 9:49 am

What is binary search? write a C program for binary search

(@prakash)
Eminent Member
Joined:2 years  ago
Posts: 20
16/02/2017 2:47 pm

C program for binary search:

#include <stdio.h>

int main()

{

int c, n, first, last, mid, search, a[250];

scanf("%d",&n);

printf("Enter the elements one by one\n", n);

for ( c = 0 ; c < n ; c++ )

{

scanf("%d",&a[c]);

}

printf("Enter the element to be searched\n");

scanf("%d",&search);

first = 0;

last = n - 1;

mid = (first+last)/2;

while( first <= last )

{

if ( a[mid] < search )

{

first = mid + 1;

}

else if ( a[mid] == search )

{

printf("%d is found at the location %d.\n", search, mid+1);

break;

}

else

{

last = mid - 1;

}

mid = (first + last)/2;

}

if ( first > last )

return 0;

}

Output:

3

Enter the elements one by one

5

3

8

Enter the element to be searched

3

3 is found at the location 2.

(@rameez)
New Member
Joined:1 year  ago
Posts: 1
26/07/2017 7:57 am

I am using the following query and I would need help from professionals working in reputed enterprise grade software development companies so that I can get best examples of coding. The idea of getting help from professionals working in top firms is because it is a fact that experience teaches a lot of lessons and learning.

What is binary search?

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array.

Coding

```/* * C program to accept N numbers sorted in ascending order * and to search for a given number using binary search. * Report success or failure. */#include <stdio.h> void main(){    int array[10];    int i, j, num, temp, keynum;    int low, mid, high;     printf("Enter the value of num \n");    scanf("%d", &num);    printf("Enter the elements one by one \n");    for (i = 0; i < num; i++)    {        scanf("%d", &array[i]);    }    printf("Input array elements \n");    for (i = 0; i < num; i++)    {        printf("%d\n", array[i]);    }    /*  Bubble sorting begins */    for (i = 0; i < num; i++)    {        for (j = 0; j < (num - i - 1); j++)        {            if (array[j] > array[j + 1])            {                temp = array[j];                array[j] = array[j + 1];                array[j + 1] = temp;            }        }    }    printf("Sorted array is...\n");    for (i = 0; i < num; i++)    {        printf("%d\n", array[i]);    }    printf("Enter the element to be searched \n");    scanf("%d", &keynum);    /*  Binary searching begins */    low = 1;    high = num;    do    {        mid = (low + high) / 2;        if (keynum < array[mid])            high = mid - 1;        else if (keynum > array[mid])            low = mid + 1;    } while (keynum != array[mid] && low <= high);    if (keynum == array[mid])    {        printf("SEARCH SUCCESSFUL \n");    }    else    {        printf("SEARCH FAILED \n");    }}
\$ cc pgm22.c\$ a.outEnter the value of num5Enter the elements one by one2390561558Input array elements2390561558Sorted array is...1523565890Enter the element to be searched58SEARCH SUCCESSFUL \$ a.outEnter the value of num4Enter the elements one by one1986545Input array elements1986545Sorted array is...1456598Enter the element to be searched6SEARCH FAILED```

(@premworld90)
New Member
Joined:1 year  ago
Posts: 1
22/09/2017 5:57 pm

Hi thiyagarajan,

Go here Binary Search Program in c

Share: