您的位置:首页 > 编程语言 > C语言/C++

二分查找算法的C语言实现

2015-10-07 23:38 253 查看
非递归的方法,所查找的数组是升序排列的。
// 二分查找算法
int simpleSearch(int array[], int len, int value) {
int minIndex = 0, maxIndex = len;
int index = minIndex;
do {
if (array[index] == value) {
return index;
break;
} else if (value < array[index]) {
maxIndex = index;
} else if (value > array[index]) {
minIndex = index;
}
index = maxIndex - minIndex;
index = minIndex + index / 2 + index % 2;
} while (index < maxIndex);
return  -1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二分查找 算法