Console.WriteLine($"二分查找{BinarySearch(3,new int[21]{1,3,5,6,7,8,9,10,10,11,12,13,14,15,17,18,19,20,21,22,23})}"); Console.ReadLine(); //有序序列二分查找 static int BinarySearch(int searchKey, int[] list) { var low = 0; var high = list.Length; for (; low <= high;) { var mid = (low + high) / 2; if (list[mid] == searchKey) { return mid; } else if (list[mid] >searchKey) { high = mid - 1; } else if (list[mid] < searchKey) { low= mid + 1; } } return -1; }
最好时间复杂度1,最坏时间复杂度logn;
空间复杂度n
标签:二分,int,复杂度,mid,算法,查找,low From: https://www.cnblogs.com/lixiang1998/p/17928979.html