How does Python insertion sort work?

前端 未结 21 1696
南方客
南方客 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条回答
  •  萌比男神i
    2020-12-10 03:28

    Insertion Sort via Recursion:

    k=1# def insertionsort(a): # should be written before k=1
    c=a[:]
    def swap(k,c,a):
        if c[k-1] > c[k]:
            c[k-1] = a[k]
            c[k] = a[k-1]
            a = c[:]
            if max(c[:k])!= c[k-1]:
                swap(k-1,c,a)
        if k < len(a)-1:
           return swap(k + 1, c,a)
        return c
    return swap(k,c,a)
    
    print(insertionsort(b))
    

提交回复
热议问题