12 插入排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhou_pp/article/details/86077110

插入排序

Description

实现插入排序。

Input

输入的每一行代表一个数组,其中的值用空格隔开,第一个值表示数组的长度。

Output

输出排序的数组,用空格隔开,末尾不要空格。

Sample Input 1 

13 24 3 56 34 3 78 12 29 49 84 51 9 100

Sample Output 1

3 3 9 12 24 29 34 49 51 56 78 84 100

def  chooseSort(arr):
    length=len(arr)
    for i in range(0,length-1):
        j=int(i)
        while j>-1:
            temp=0
            if(int(arr[j+1])<int(arr[j])):
               temp=arr[j+1]
               arr[j+1]=arr[j]
               arr[j]=temp
            j-=1
    return arr

if '_main_':
 try:
     while True:
         A=[]
         B=[]
         s=input()
         d=s.split(' ')
         A.extend(d)
         A.remove(A[0])
         B=chooseSort(A)
         for h in range(0,len(B)-1):
             print(B[h],end=' ')
         print(B[len(B)-1])
 except EOFError:
     exit()

猜你喜欢

转载自blog.csdn.net/zhou_pp/article/details/86077110