How does Python insertion sort work?

前端 未结 21 1751
南方客
南方客 2020-12-10 02:54

Here\'s a Python implementation of insertion sort, I tried to follow the values on paper but once the counting variable i gets bigger than len(s) I don\'t know what to do, h

21条回答
  •  甜味超标
    2020-12-10 03:32

    def insertionsort(list):
        for i in range(1,len(list)):
            temp=list[i]
            j=i-1
            while temp<+list[j] and j>=0:
                list[j+1]=list[j]
                j=j-1
            list[j+1]=temp
        return list
    list=eval(raw_input('Enter a list:'))
    print insertionsort(list)
    

    This will help you.

提交回复
热议问题