Python堆排序
- def heapSort(alist):
- if alist == None or len(alist) == 0:
- return
- length = len(alist)
- output = []
- for i in range(length):
- tempLen = len(alist)
- for j in range(tempLen//2-1, -1, -1):
- preIndex = j
- preVal, heap = alist[preIndex], False
- while 2 * preIndex < tempLen - 1 and not heap:
- curIndex = 2 * preIndex + 1
- if curIndex < tempLen - 1:
- if alist[curIndex] < alist[curIndex+1]:
- curIndex += 1
- if preVal >= alist[curIndex]:
- heap = True
- else:
- alist[preIndex] = alist[curIndex]
- preIndex = curIndex
- alist[preIndex] = preVal
- output.insert(0, alist.pop(0))
- return output
-
- test = [2, 6, 5, 9, 10, 3, 7]
- print(heapSort(test))
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。