标签:div print 操作 sorted pop pen sub 排序 find
## 选择排序需要执行n次时间为O(n)的操作,所以总时间为O(n2)
1 def find_smallest(arr): 2 """return the index of the smallest element""" 3 smallest = arr[0] 4 smallest_index = 0 5 for i in range(0, len(arr)): 6 if arr[i] < smallest: 7 smallest = arr[i] 8 smallest_index = i 9 return smallest_index 10 11 def selection_sort(arr): 12 sorted_arr = [] 13 for i in range(0, len(arr)): 14 smallest_index = find_smallest(arr) 15 sorted_arr.append(arr.pop(smallest_index)) 16 return sorted_arr 17 18 print(selection_sort([2,3,4,5,7,2,21,-2,-4,-9])) 19 # [-9, -4, -2, 2, 2, 3, 4, 5, 7, 21]
标签:div print 操作 sorted pop pen sub 排序 find
原文地址:https://www.cnblogs.com/hycstar/p/9345491.html