#include <stdio.h> int findInsertIndex(int array[],int length,int key) { int min=0; int max=length-1; while(max>=min) { int mid=(max+min)/2; if(array[mid]==key) { return mid; }else if (array[mid]>key) { max=mid-1; }else { min=mid+1; } } return min; } int main(int argc, const char * argv[]) { int nums[]={1,3,6,8,10}; int key=3; int length=sizeof(nums)/sizeof(int); int index=findInsertIndex(nums,length,key); printf("%d\n",index); return 0; } |