【Python 训练营】N_18 插入排序

简介: 【Python 训练营】N_18 插入排序

题目

列表L = [3,2,5,6,1,3,8,1,9],通过元素插入实现从小到大排列。

分析

比较两两位置下元素大小,进行互换,类似冒泡排序

答案

def insert_sort(lists):
    for i in range(len(lists)):
        position=i
        while position>0:
            if lists[position]<lists[position-1]:
                lists[position],lists[position-1]=lists[position-1],lists[position]
            position-=1
        # print(lists)
    return lists

L = [3,2,5,6,1,3,8,1,9]
print( insert_sort(L))
目录
打赏
0
0
0
0
38
分享
相关文章
|
9月前
|
【Python 训练营】N_17 冒泡排序
【Python 训练营】N_17 冒泡排序
39 2
|
9月前
|
【Python 训练营】N_16 二分法查找
【Python 训练营】N_16 二分法查找
36 1
|
9月前
|
【Python 训练营】N_15 列表元素去重
【Python 训练营】N_15 列表元素去重
49 1
|
9月前
|
【Python 训练营】N_14 文件查找和替换
【Python 训练营】N_14 文件查找和替换
44 2
|
9月前
|
【Python 训练营】N_13 遍历字符串
【Python 训练营】N_13 遍历字符串
60 2
|
9月前
|
【Python 训练营】N_12 打印菱形图案
【Python 训练营】N_12 打印菱形图案
69 1
|
9月前
|
【Python 训练营】N_11 模拟进度条
【Python 训练营】N_11 模拟进度条
35 1
|
9月前
|
【Python 训练营】N_10 出租车计费
【Python 训练营】N_10 出租车计费
97 0
|
9月前
|
【Python 训练营】N_9 自由落体运动
【Python 训练营】N_9 自由落体运动
39 0
|
9月前
|
【Python 训练营】N_8 打印阿姆斯特朗数
【Python 训练营】N_8 打印阿姆斯特朗数
28 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等