How does Python insertion sort work?

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

    def ins(l):
        for i in range(1, len(l)):
            current_index = i
            current_value = l[i]
            while current_index > 0 and current_value < l[current_index - 1]:
                l[current_index] = l[current_index - 1]
                current_index -= 1
            l[current_index] = current_value
        print(l)
    

提交回复
热议问题