/* 二分法查找 */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* 二分法查找是一种在有序数组中查找特定元素的搜索算法 二分法查找的时间复杂度O(logn) */ //递归算法 int recurbinary(int *a, int key, int low, int high) { int mid; if (low > high) { return -1; } mid = low + (high - low) / 2; if (a[mid] == key) { return mid; } else if (a[mid] > key) { return recurbinary(a, key, low, mid - 1); } else { return recurbinary(a, key, mid + 1, high); } } //非递归算法 int binary(int *a, int key, int n) { int left = 0, right = n - 1, mid = 0; mid = left + (right - left) / 2; while (left < right && a[mid] != key) { if (a[mid] < key) { left = mid + 1; } else if (a[mid] > key) { right = mid; } mid = (left + right) / 2; } if (a[mid] == key) { return mid; } return -1; } int test() { int a[] = { 1,2,3,4,5,6,7,8,9,12,13,45,67,89,99,101,111,123,134,565,677 }; int b[] = { 677,1,7,11,67 }; int i = 0; for (i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d\n", binary(a, b[i], sizeof(a) / sizeof(a[0]))); } return 0; } int main() { test(); printf("-----ok------\n"); getchar(); return 0; }
来源:https://www.cnblogs.com/zhanggaofeng/p/10852810.html