How does Python insertion sort work?

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

        def insertie(x):
        nrc=0
        nrm=0
        for i in range(0,len(x)):
            aux=x[i]
            nrm=nrm+1
            j=i-1
            while j >= 0 and aux < x[j]:
                nrc=nrc+1
                x[j+1]=x[j]
                nrm=nrm+1
                j=j-1
            nrc=nrc+1
            x[j+1]=aux
            nrm=nrm+1
        return x
    
    x=[7,5,4,9,1,4,0,1]
    print insertie(x)
    

提交回复
热议问题