查找
二分查找:
完整代码:
#include<stdio.h> #include<stdlib.h> int main() { int array[6]={1,2,5,6,7,8}; int arrayLengh=6; int low=0; int high=arrayLengh-1; int index=-1; int mid; int target; printf("input you look for:\n"); scanf("%d",&target); while(low<=high) { mid=(low+high)/2; if(array[mid]==target) { index=mid; break; } else if(array[mid]<target) { low=mid+1; } else { high=mid-1; } } if(index==-1) printf("NOT FOUND!\n"); else printf("FOUND IT! the index=%d\n",index); return 0; }