我照着《算法图解》中选择排序的代码敲了一遍,
运行时却提示报错,错误码如下:
UnboundLocalError: local variable 'samllest_index' referenced before assignment
代码如下
def findSmallest (arr):
smallest = arr[0]
smallest_index = 0
for i in range(1,len(arr)):
if arr[i] < smallest:
samllest = arr[i]
samllest_index = i
return samllest_index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr
array = [5,3,6,2,10]
print("数组最小值的下标是:{}".format(findSmallest(array)))
print (selectionSort(array))
查了好久,不知道问题所在,求解!!!