How does Python insertion sort work?

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

    A simple combination of list slicing and bubble sort

    s = [54,26,93,17,77,31,44,55,20]
    
    for i in range(1,len(s)+1):
        b = s[0:i]
        a = b
        count = 0
    
        while count= a[j+1-len(a)]:
                    temp = a[j]
                    a[j] = a[j+1-len(a)]
                    a[j+1-len(a)] = temp
    
            count = count+1
    print a    
    

提交回复
热议问题