【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))
相关文章
|
3月前
|
Python
【Python 训练营】N_17 冒泡排序
【Python 训练营】N_17 冒泡排序
25 2
|
3月前
|
Python
【Python 训练营】N_14 文件查找和替换
【Python 训练营】N_14 文件查找和替换
26 2
|
3月前
|
Python
【Python 训练营】N_13 遍历字符串
【Python 训练营】N_13 遍历字符串
36 2
|
3月前
|
Python
【Python 训练营】N_5 斐波那契数列
【Python 训练营】N_5 斐波那契数列
26 2
|
3月前
|
Python
【Python 训练营】N_16 二分法查找
【Python 训练营】N_16 二分法查找
20 1
|
3月前
|
Python
【Python 训练营】N_15 列表元素去重
【Python 训练营】N_15 列表元素去重
31 1
|
3月前
|
Python
【Python 训练营】N_12 打印菱形图案
【Python 训练营】N_12 打印菱形图案
30 1
|
3月前
|
Python
【Python 训练营】N_11 模拟进度条
【Python 训练营】N_11 模拟进度条
18 1
|
3月前
|
Python
【Python 训练营】N_7 打印水仙花数
【Python 训练营】N_7 打印水仙花数
25 1
|
3月前
|
Python
【Python 训练营】N_10 出租车计费
【Python 训练营】N_10 出租车计费
49 0