查找函数,为看的明显一点,加了注释
def BinarySearch(listNum, aimNum): lowpos = 0 highpos = len(listNum) - 1 print("当前头坐标:lowpos = {}, 尾坐标 highpos={}".format(lowpos, highpos)) i = 0 while(1): i = i + 1 print("第 %d 趟"%i) if(lowpos <= highpos): midpos = lowpos + (highpos - lowpos) // 2 midNum = listNum[midpos] print(" ", end='') print(listNum, aimNum) print("当前头坐标lowpos = {},尾坐标highpos={},新中值坐标midpos = {},新中值midNum={}".format(lowpos, highpos, midpos , midNum)) if midNum == aimNum: print(listNum, aimNum) print("找到! 当前头坐标lowpos = {},尾坐标highpos={},新中值坐标midpos = {},新中值midNum={}".format(lowpos, highpos, midpos,midNum)) return midpos elif midNum < aimNum: lowpos = midpos + 1 print(" ", end='') print(listNum, aimNum) print(" 新中值小于目标值 头坐标后移1:当前头坐标lowpos = {},尾坐标highpos={},新中值坐标midpos = {},新中值midNum={}".format(lowpos, highpos, midpos,midNum)) else: highpos = midpos - 1 print(" ", end='') print(listNum, aimNum) print(" 新中值小于目标值 尾坐标前移1:当前头坐标lowpos = {},尾坐标highpos={},新中值坐标midpos = {},新中值midNum={}".format(lowpos, highpos, midpos,midNum)) return None