我一直在进行二进制插入排序,但是遇到一个问题。它一直告诉我在第二个for循环之前,在“ binary_insertion_sort”函数中将“ NoneType对象不能解释为整数”。谁能解释我的代码出了什么问题,并告诉我其中是否存在逻辑错误?
def binary_search(n, bin_list, low, high):
print(low,high)
if high - low <= 1:
if n < bin_list[low]:
return low - 1
elif n > bin_list[high]:
print('d',n,high)
return high + 1
elif n == bin_list[low]:
return low
elif n== bin_list[high]:
return high
else:
print('c',low)
return low+1
mid = (low+high)//2
print('a',mid)
if n < bin_list[mid]:
binary_search(n, bin_list, 0, mid-1)
elif n > bin_list[mid]:
binary_search(n, bin_list, mid+1, high)
else:
return mid
binary_insertion_sort部分
def binary_insertion_sort(text):
sorted_list = text.split()
for num in range(1, len(sorted_list)):
unsorted = sorted_list[num]
print(sorted_list)
index = binary_search(unsorted, sorted_list, 0, len(sorted_list)-1)
for j in range(num, index, -1):
print(j)
sorted_list[j] = sorted_list[j-1]
sorted_list[index-1] = num
return sorted_list
a = binary_insertion_sort('1 2 3 4 5')
我认为您在binary_search函数中缺少递归情况的回报。