#include <stdio.h>

int binarySearch(int a[], int size, int key) {
    int left = 0;
    int right = size - 1;
    while (left <= right) {
        int middle = (left + right) / 2;
        if (key == a[middle])
            return middle;
        else if (key > a[middle])
            left = middle + 1;
        else
            right = middle - 1;
    }
    return -1;
}

int main() {
    int a[] = {1, 3, 5, 7, 9};
    int size = sizeof(a) / sizeof(a[0]);
    int key = 7;
    int index = binarySearch(a, size, key);
    if (index != -1)
        printf("元素索引是 %d\n", index);
    else
        printf("没有找到\n");
    return 0;
}
