二分搜尋法

 avatar
user_3763047219
c_cpp
2 years ago
914 B
5
Indexable
Never
#define _CRT_SCURE_NO_WARNINGS
#include <iostream>

int binarysearch(int data[], int search, int n);

int binarysearch(int data[], int search, int n) {
    int low = 0, high = n - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (data[mid] == search) {
            return mid;
        }
        else if (data[mid] > search) {
            high = mid - 1;
        }
        else if (data[mid] < search) {
            low = mid + 1;
        }
    }
    return -1;
}


#include <stdio.h>

int main() {
    int search, ans;
    int data[] = { 3, 7, 14, 20, 23, 32, 41, 44, 56, 57, 73, 89, 93 };
    scanf("%d", &search);
    ans = binarysearch(data, search, sizeof(data) / sizeof(int));
    if (ans < 0) {
        printf("找不到 %d\n", search);
    }
    else {
        printf("在第 %d 筆資料找到 %d\n", ans + 1, search);
    }

    return 0;
}