标签:c 折半查找
#include<stdio.h> #include<stdlib.h> int search(int a[],int i,int len) { int start,end,mid; start=0; end=len-1; if((i<a[start])||(i>a[end])) { return -1; } while(start<=end) { mid=(start+end)/2; if(a[mid]<i) { start=mid+1; } else if(a[mid]>i) { end=mid-1; } else if(a[mid]==i) { return mid; } else { return -1; } } } int main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; int i,len,c; len=sizeof(a)/sizeof(a[0]); printf("输入要查找的数"); scanf("%d",&i); c=search(a,i,len); if(c==-1) { printf("没有该数"); } else { printf("%d",c); } system("pause"); return 0; }
本文出自 “liveyoung” 博客,转载请与作者联系!
标签:c 折半查找
原文地址:http://10707042.blog.51cto.com/10697042/1702646