二分查找
代码
//
// main.c
// algorithm
//
// Created by athlonreg on 17/01/2018.
// Copyright © 2018 athlonreg. All rights reserved.
//
#include <stdio.h>
#define n 5
int arr[n]={1,2,3,4,5};
int BinarySearch(int a[], int low, int high, int k){
while(low<high){
int mid=(low+high)/2;
if(a[mid]==k)
return mid;
else if(a[mid]>k)
return BinarySearch(a, low, mid-1, k);
else
return BinarySearch(a, mid+1, high, k);
}
return -1;
}
int main(int argc, const char * argv[]) {
int x=4;
printf("%d\n", BinarySearch(arr, 0, n-1, x));
return 0;
}
运行结果
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 套陆的博客!
评论
TwikooUtterances