插入排序算法
插入排序算法
# -*- coding: utf-8 -*-
# @Time : 2022/3/9 18:38
# @Author : chuqianyu
# @FileName: 011-插入排序.py
# @Software: PyCharm
# @Blog :https://home.cnblogs.com/u/chuqianyu
class Solution:
def charuSort(self, arr): # 插入排序
n = len(arr)
for i in range(1, n):
for j in range(i - 1, -1, -1):
temp = 0
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr
if __name__ == "__main__":
arr = list(map(int, input().strip().split())) # "请输入数组长度,用空格隔开:"
s = Solution()
arr2 = s.charuSort(arr)
print(arr2)
for i in range(len(arr2)):
print(arr2[i], end=" ")
# print([0] * 2# 产生1行2列的列表
作者:楚千羽
出处:https://www.cnblogs.com/chuqianyu/
本文来自博客园,本文作者:楚千羽,转载请注明原文链接:https://www.cnblogs.com/chuqianyu/p/16247763.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利!