103. 二分搜尋法

 avatar
user_6817964
c_cpp
2 years ago
677 B
1
Indexable
Never
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
			low = mid + 1;
	}
	return -1;
}

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;
}