python堆排

def duisort(s):
    builtheap(s) print('here')
    lens = len(s)  for i in range(len(s)):
        s[0],s[lens-i-1] = s[lens-i-1],s[0]
        duiadjust(s,0,lens-i-2) def builtheap(s):
    lens = len(s) print('here') for i in range(lens//2-1,-1,-1): print(i) print('here')
        duiadjust(s,i,lens) def duiadjust(s,i,lens):
    left = i*2+1  right = i*2+2  if i<lens//2:
        max  = i if right<lens and s[max]<s[right]:
            max = right if left<lens and s[max]<s[left]:
            max= left  if max!=i:
            s[i],s[max] = s[max],s[i]  if max<lens//2 and max!=i:
            duiadjust(s,max,lens) if __name__=='__main__':
    s = [1,26,3,5,4.8,2,5,2]
    duisort(s) print(s)

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务