Binary Search

Published on January 2017 | Categories: Documents | Downloads: 40 | Comments: 0 | Views: 198
of 1
Download PDF   Embed   Report

Comments

Content

#include <stdio.h>
int main()
{
int c, first, last, middle, n, cauta, a[100];
printf("Cate elemente ?\n");
scanf("%d",&n);
printf("Introdu %d numere \n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&a[c]);
printf("Ce valoare cautati ?\n");
scanf("%d",&cauta);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( a[middle] < cauta )
first = middle + 1;
else if ( a[middle] == cauta )
{
printf( "%d gasit pe pozitia %d.\n", cauta, middle+1);
break;
}
else
last = middle - 1;
middle = ( first + last )/2;
}
if ( first > last )
printf( " Nu s-a gasit %d, nu este prezent in tablou.\n ", cauta );
getch();
return 0;
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close