How does Python insertion sort work?

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

    def insertion(x):
        for i in range(1, len(x)):
            while x[i - 1] > x[i] and i >= 0:
                x[i - 1], x[i] = x[i], x[i - 1]
                i -= 1
        return x
    

    Something like that

提交回复
热议问题