标签:折半查找
#include <stdio.h> int half_search(int arr[], int first, int last, int n) { int mid = 0; while (first <= last) { mid = (first + last) / 2; if (n < arr[mid]) { last = mid - 1; } else if (n>arr[mid]) { first = mid + 1; } else { return 1; } } return -1; } int main() { int arr[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; int i; while (1) { scanf("%d", &i); if ((half_search(arr, 0, sizeof(arr) / sizeof(arr[0]) - 1, i) == -1)) { printf("not exist\n"); } else { printf("%d\n", i); } } return 0; }
标签:折半查找
原文地址:http://10706198.blog.51cto.com/10696198/1760033