pyhton选择排序代码
时间: 2023-09-23 19:12:46 浏览: 53
以下是Python实现选择排序的代码:
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
# 测试代码
arr = [64, 25, 12, 22, 11]
print(selection_sort(arr)) # 输出 [11, 12, 22, 25, 64]
阅读全文