编程语言
首页 > 编程语言> > 插入排序算法

插入排序算法

作者:互联网

插入排序算法

# -*- 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列的列表

标签:__,arr,temp,插入排序,算法,arr2,print
来源: https://www.cnblogs.com/chuqianyu/p/16247763.html