时间复杂度O(log ₂ N)
局限:查找有序的数组并且无重复元素的数组中的一个元素。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//二分查找
int main()
{
	char arr[10] = { 1,2,3,4,5,6,7,8,9,10 };//7
	int k = 4;
	int sz = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = sz - 1;
	while (left <= right)
	{
		int mid = left + (right - left) / 2;
		if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else
		{
			printf("找到了,下标是%d", mid);
			break;
		}
	}
	if (left > right)
		printf("找不到");
	return 0;
}