编辑代码

#include <stdio.h>

int binary_search(int arr[], int size, int k) {
    int low = 0;
    int high = size - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] < k) {
            low = mid + 1;
        } else if (arr[mid] > k) {
            high = mid - 1;
        } else {
            return mid;
        }
    }
    return -1;
}

int main() {
    int arr[] = {6, 17, 25, 34, 52, 63, 85};
    int size = sizeof(arr) / sizeof(arr[0]);
    int k = 6;
    int result = binary_search(arr, size, k);
    if (result != -1) {
        printf("Element %d found at index %d\n", k, result);
    } else {
        printf("Element %d not found\n", k);
    }
    return 0;
}