遞迴二分法查詢數字

2021-09-10 09:25:41 字數 4489 閱讀 3839

方法:正確的

l = [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88]

def find(l,aim,start = 0,end = none):

end = len(l) if end is none else end

mid_index = (end - start)//2 + start

if start <= end:

if l[mid_index] < aim:

return find(l,aim,start =mid_index+1,end=end)

elif l[mid_index] > aim:

return find(l, aim, start=start, end=mid_index-1)

else:

return mid_index

else:

return '找不到這個值'

ret= find(l,66)

print(ret)

拆分分析:

# 執行:

def find(l,aim,start = 0,end = none): # l,66,

end = len(l) if end is none else end #25

mid_index = (end - start)//2 + start #12

if start <= end:#0<25

if l[mid_index] < aim: #l[12]<66 成立

return find1(l,aim,start =mid_index+1,end=end) # l,66,start=13,end=25 第一次呼叫 #接收返回值17

elif l[mid_index] > aim:

return find1(l, aim, start=start, end=mid_index-1)

else:

return mid_index

else:

return '找不到這個值'

#第一次呼叫

def find1(l,aim,start = 12+1,end = 25): # l,66,13,25

end = len(l) if end is none else end# 25

mid_index = (end - start)//2 + start #(25-13)//2+13=19

if start <= end: #13<=25

if l[mid_index] < aim:

return find1(l,aim,start =mid_index+1,end=end)

elif l[mid_index] > aim: #l[19]>66 成立

return find2(l, aim, start=start, end=mid_index-1) #l,66,start=13,end=18第二次呼叫 #接收返回值17

else:

return mid_index

else:

return '找不到這個值'

#第二次呼叫

def find2(l,aim,start = 13,end = 19-1): # l,66,13,18

end = len(l) if end is none else end# 18

mid_index = (end - start)//2 + start #(18-13)//2+13=15

if start <= end: #13<=18

if l[mid_index] < aim: #l[15]<66 成立

return find1(l,aim,start =mid_index+1,end=end) #l,66,start=15+1,end=18第三次呼叫 #接收返回值17

elif l[mid_index] > aim:

return find2(l, aim, start=start, end=mid_index-1)

else:

return mid_index

else:

return '找不到這個值'

#第三次呼叫

def find1(l,aim,start = 15+1,end = 18): # l,66,15+1,18

end = len(l) if end is none else end# 18

mid_index = (end - start)//2 + start #(18-16)//2+16=17

if start <= end: #13<=18

if l[mid_index] < aim: #l[17]<66 不成立

return find(l,aim,start =mid_index+1,end=end) #l,66,start=15+1,end=18第三次呼叫

elif l[mid_index] > aim: #l[17]>66 不成立

return find(l, aim, start=start, end=mid_index-1)

else:#l[17]==66 成立

return mid_index#返回值索引17

else:

return '找不到這個值'

ret= find(l,66)

print(ret)

並且分析找不到的解析呼叫

s1=[2,5,8,9,11,13,18,19,25,26,27,29,30,31,35,36,39,44,50]

def find_num1(s,num,start=0,end=none): #s1 ,20,0,

end=len(s) if end is none else end# 18

index =( end-start)//2+start # 18//2=9

if start<=end:

if num > s[index]:

#new_s =s[index+1:]

return find_num1(s, num,start=index+1,end=end)

elif num s[index]: #20>s[2]

#new_s =s[index+1:]

find_num1(s, num,start=index+1,end=end) #s1,20,start=3,end=8

elif num s[index]: #20>s[5]

#new_s =s[index+1:]

find_num1(s, num,start=index+1,end=end) #s1,20,6,8

elif num s[index]: #20>s[4]

#new_s =s[index+1:]

find_num1(s, num,start=index+1,end=end) #s1,20,5,8

elif num s[index]: #20>s[6]

#new_s =s[index+1:]

find_num1(s, num,start=index+1,end=end) #s1,20,7,8

elif num s[index]: #20>s[7]

#new_s =s[index+1:]

find_num1(s, num,start=index+1,end=end) #s1,20,8,8

elif num s[index]: # 20>s[7]

# new_s =s[index+1:]

find_num1(s, num, start=index + 1, end=end) # s1,20,9,8

elif num < s[index]:

# new_s = s[:index]

find_num1(s, num, start=start, end=index - 1)

else:

print('找到了', index, s[index])

# 第八次呼叫

def find_num1(s, num, start=9, end=8): ## s1,20,9,8

end = len(s) if end is none else end # 8

index = (end - start) // 2 + start # (8-9)//2+9

if start<=end:

if num > s[index]: # 20>s[7]

# new_s =s[index+1:]

find_num1(s, num, start=index + 1, end=end)

elif num < s[index]:

# new_s = s[:index]

find_num1(s, num, start=start, end=index - 1)

else:

print('找到了', index, s[index])

else:

print('找不到')

C 二分法查詢,遞迴二分法

用二分法來求需要查詢的值.includeusing namespace std 查詢key元素是否存在 int findkey const int buf 100 const int ilen,const int key else right left mid 1 查詢失敗 return 1 查詢k...

遞迴二分法查詢

二分法使用了折半查詢的思想,不斷的變化,陣列下標的起始位置 begin 和終止位置 end 來進行搜尋。我們使用兩種演算法,解決二分查詢 public class solution system.out.println solution.sort number,0,number.length,56 ...

二分法查詢 遞迴

需求 查詢陣列中某個元素的下標 實現方法 二分法查詢 折半查詢,每次把錶分成兩半,因為已經排好序,所以每次只需要和中間的數比較,就能確定要查詢的值在哪一半,然後不斷分成兩半,直到匹配,如果沒有找到,則表示沒有該元素 public static int find int x int min 0 int...