使用python编写一个排序
以下是一个选择排序的Python实现:
def selection_sort(arr): for i in range(len(arr)): min_index = i for j in range(i+1, len(arr)): if arr[j] < arr[min_index]: min_index = j arr[i], arr[min_index] = arr[min_index], arr[i] return arr
测试
arr = [64, 25, 12, 22, 11] print("排序前:", arr) sorted_arr = selection_sort(arr) print("排序后:", sorted_arr)
输出:
排序前: [64, 25, 12, 22, 11]
排序后: [11, 12, 22, 25, 64]
原文地址: https://www.cveoy.top/t/topic/rAl 著作权归作者所有。请勿转载和采集!