Binary
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{ int a[100];
int i,j,n,low,high,mid,tg,t,k=0;
clrscr();
printf("
nhap n : "); scanf("%d",&n);
for(i=1;i<=n;i++)
{ printf("
a[%d]:=",i);
scanf("%d",&a[i]);
}
for(i=1;i<=(n-1);i++)
for(j=i+1;j<=n;j++)
if(a[i]>a[j])
{ tg=a[j];
a[j]=a[i];
a[i]=tg;
}
printf("
day so sau khi sap xep la :
");
for(i=1;i<=n;i++)
printf("%7d",a[i]);
printf("
nhap so can tim : "); scanf("%d",&t);
low=1; high=n; mid=high;
while(a[mid]!=t)
{ if (t>a[mid]) low=mid;
else high=mid;
mid=(high+low)/2;
if (low+1>=high) { k=1; break; }
}
if(k==0) printf("
so can tim la so thu %d",mid);
else printf("
so nay khong ton tai trong list");
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro