How does Python insertion sort work?

前端 未结 21 1705
南方客
南方客 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:27

    def insertionSort(alist):
        for index in range(1, len(alist)):
    
            currentvalue = alist[index]
            position = index
    
            while position > 0 and alist[position-1] > currentvalue:
                alist[position] = alist[position-1]
                print(alist)
                position -= 1
    
            alist[position] = currentvalue
    
    alist = [int(i) for i in input().split()]       
    insertionSort(alist)
    

提交回复
热议问题