answersLogoWhite

0


Best Answer

/* 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

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago
  1. #include
void main() { int a[7],i,t,m=0; printf("Enter your ascending sorted Numbers:"); for(i=0;i<7;i++) { scanf("%d",&a[i]); } do { printf("\nEnter your Number that you want to search:"); scanf("%d",&t); m=(0+7)/2; if(t!=a[m]) { if(t>a[m]) { m=(m+1+7)/2; if(t>a[m]) { printf("%d",m+1); } else if(ta[m]) { printf("%d",m+1); } else if(t
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program for binary searching?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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


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(); } }


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.


How do you convert from assembly to binary in c plus plus?

Use inline assembly instructions. Then compile your C++ program to produce the machine code.


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