#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018-01-31 20:29 # @Author : hhj# @Site : # @File : 递归实现二分查找.py# def func(arg1,arg2,stop):# if arg1==0:# print(arg1,arg2)# arg3=arg1+arg2# print(arg3)# if arg3<stop:# func(arg2,arg3,stop) #递归### func(0,1,100) def binary_search(data_source,find_n): mid=int(len(data_source)/2) if len(data_source)>1: # 递归结束条件 if data_source[mid]>find_n: #要找的值在左边,即:data in left print("data in left of [%s]" %data_source[mid]) # print(data_source[:mid]) binary_search(data_source[:mid],find_n) #做切片 elif data_source[mid] < find_n: # 要找的值在左边,即:data in right print("data in right of [%s]" % data_source[mid]) # print(data_source[mid:]) binary_search(data_source[mid:],find_n) # 做切片 else: #若 print("found data:",data_source[mid]) else: print("connot find .....") if __name__=="__main__": data=list(range(0,7000000)) # print(data) binary_search(data,35666)
原文地址:https://www.cnblogs.com/hhjwqh/p/8394958.html
时间: 2024-11-09 01:41:39