# python算法双指针问题：二分查找

```import math
a_list = [2, 5, 23, 45, 67, 89, 90, 123, 234, 345, 567, 7890, 12132]

guess_no = 67

start, end = 0, len(a_list)

while end - start > 1:
mid = (end + start) // 2
print(start, end, mid, a_list[mid])
if a_list[mid] == guess_no:
break
elif a_list[mid] < guess_no:
start = mid + 1
else:
end = mid
else:
if a_list[start] == guess_no:
print(‘find it‘)

# ==================上面为书，下面为自己=================

start, end = 0, len(a_list)
while start < end:
mid = math.floor((start + end) / 2)
print(start, end, mid, a_list[mid])
if guess_no < a_list[mid]:
end = mid
elif guess_no == a_list[mid]:
break
else:
start = mid + 1

```0 13 6 90
0 6 3 45
4 6 5 89
find it
67
0 13 6 90
0 6 3 45
4 6 5 89
4 5 4 67
67

Process finished with exit code 0```

## 算法——基础篇——二分查找

二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表.     首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功:否则利用中间位置记录将表分成前.后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表.重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功

## 查找算法总结（二分查找/二叉查找树/红黑树/散列表）

1.二分查找 二分查找时,先将被查找的键和子数组的中间键比较.如果被查找的键小于中间键,就在左子数组继续查找,如果大于中间键,就在右子数组中查找,否则中间键就是要找的元素. /** * 二分查找 */ public class BinarySearch { public static int find(int[] array, int key) { int left = 0; int right = array.length - 1; // while (left <= right)不能改为<

## python s12 day4 算法基础之二分查找

def binary_search(data_source,find_n): mind=int(len(data_source)/2) if len(data_source)>=1: if data_source[mid]>find_n: print("data in left of [%s]"%sdata_souerce[mid]) //print(data_souerce[:mid]    binary_search(data_source[:mid],find_n)

## 算法—8.有序数组中的二分查找

1.具体算法 /** * 算法3.2 二分查找(基于有序数组) * Created by huazhou on 2015/11/29. */ public class BinarySearchST<Key extends Comparable<key>, Value> { private Key[] keys; private Value[] vals; private int N; public BinarySearchST(int capacity){ keys = (Key[