二分查找(返回相同元素索性最小)
#include "stdio.h" int bisearch(int arr[],int n , int value) { int min = 0; int max = n-1; int mid = 0; while( min < max-1) { mid = max - (max - min)/2; if (arr[mid] > value) { max =mid; } else if(arr[mid] < value) { min = mid; } else { max = mid; } } if (arr[min] == value) { return min; } else if(arr[max] == value) { return max; } else return -1; } void main() { int arr[10] = {1,3,4,5,9,12,12,12,56,98}; printf("%d" , bisearch(arr,10,12)); }