有序数组的二分查找
NSInteger GetIndexInArr(NSArray *arr,NSNumber *num) {
NSInteger min = 0;
NSInteger max = arr.count - 1;
NSInteger mid = 0;
while (min <= max) {
mid = (min + max) / 2;
if ([arr[mid] integerValue] == num.integerValue) {
return mid;
} else if ([arr[mid] integerValue] > num.integerValue) {
max = mid - 1;
} else {
min = mid + 1;
}
}
return -1;
}
网友评论