猫史档案馆


【Python作品分享】新作品【教程贴】

用户:一起来编程吧ll一起来编程吧ll查看:0 回复:2 评论:0 创建时间:2020-06-17T12:14:15


【作品展示】

center_image

 

【作品介绍】

选择排序算法

 

【作品源代码】

List_1 = [5, 4, 3, 2, 1]
List_2 = [9, 5, 4, 3, 2, 1, 6, 2]

# 选择排序算法
def selection_sort(List):
    for j in range(len(List)):
        index = j  # 最小值的索引
        for i in range(j+1, len(List)):
            if List[i] < List[index]:
                index = i
        # 方法一
        # temp = List.pop(index)
        # List.insert(j, temp)

        # 方法二
        # temp = List[j]
        # List[j] = List[index]
        # List[index] = temp
        List[j], List[index] = List[index], List[j]

    return List

print(selection_sort(List_1))
print(selection_sort(List_2))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
lsk666666lsk666666

emotion_编程猫_点赞

点赞0


评论


FHR丨萌新滑稽FHR丨萌新滑稽

3Q

点赞0


评论