answersLogoWhite

0

/* C program for binary search: This code implements binary search in */

/* C language. It can only be used for sorted arrays, but it's fast as */

/* compared to linear search. If you wish to use binary search on an */

/* array which is not sorted then you must sort it using some sorting */

/* technique say merge sort and then use binary search algorithm to */

/* find the desired element in the list. If the element to be searched */

/* is found then its position is printed. */

#include<stdio.h>

main()

{

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

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

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

printf("Enter value to find\n");

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("%d found at location %d.\n", search, middle+1);

break;

}

else

last = middle - 1;

middle = (first + last)/2;

}

if ( first > last )

printf("Not found! %d is not present in the list.\n", search);

return 0;

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Write a program of binary heap in c or c language?

to implement operations on binary heap in c


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


Write a c program to convert binary number to decimal number?

How is this a question? Sounds like you should do more of your homework offline.


How is a C program rum?

C-source program doesn't rum, you have to compile and link it. The executable rums like any other binary program.


C program to receive floating point and convert it into binary?

scanf


How c program gets stored in the memory?

Binary object code executable.


Write a c program for creating a virus?

a c program for creat a virus


Where do we write main function in a c program?

Into the source program.


Write a c program to check whether a number is binary or not?

#include&lt;stdio.h&gt; void main() { int n; clrscr(); printf("enter a no."); scanf("%d",&amp;n); if(n%10==0n%10==1) printf("yes binary"); else printf("not binary"); getch(); } }


Write a c program to find Volume and surface area of cube?

Write a c program to compute the surface area and volume of a cube


How do you write a c program to analysis a circuit?

There is no need to write a C program for circuit analysis; there are lots of packages out there that already do that (for example, Spice).