折半查找

来源:互联网 发布:化妆品成分查询软件 编辑:程序博客网 时间:2024/05/01 17:43
/*************************       折半查找  ***********************/#include<stdio.h>#define size 15void sort(int a[],int tmp){int mid,low,high;low = 1;high = size;mid=(low+high)/2;while(low <= high){if(tmp == a[mid]){printf("find the data!!\na[%d] = %d\n",mid,a[mid]);return;}       if(tmp < a[mid]){high = mid-1;mid = (low+high)/2;}else  {low = mid+1;mid = (low+high)/2;}}printf("sorry ,the data is not exist\n");}int main(void){int a[15]={1,1,2,3,5,8,13,21,34,55,89,144,233,377,610};puts("折半查找\n");puts("please input the data you want to find");int tmp;scanf("%d",&tmp);sort(a,tmp);return 0;}


原创粉丝点击