2018-05-10

算法图解 p28 选择排序

# coding=utf-8
# Hello World program in Python
    
print "5555Hello World!\n"


def findSmallest(arr):
    smallest = arr[0]
    smallest_index = 0
    for i in range(1,len(arr)):
        print ('sala='+str(i))   #打印让你看清楚循环的次数
        if arr[i] < smallest:
            smallest = arr[i]
            smallest_index = i
    return smallest_index

def selectionSort(arr):
    newArr = []
    for i in range(len(arr)):
        smallest = findSmallest(arr)
        newArr.append(arr.pop(smallest)) #注意列表长度变小
        #newArr.append(arr[smallest])
    return newArr

print selectionSort([10,2,3,4,5,6,7,99,8,8,8,8,67,64,322,44])

你可能感兴趣的:(2018-05-10)