Skip to content

Commit 8a65c74

Browse files
committed
new file
1 parent acb19cd commit 8a65c74

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

sorting/heapq.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
4+
def ReBalance(alist, i):
5+
length = len(alist)
6+
index = i
7+
if (2 * i) < length and alist[i] < alist[2 * i]:
8+
index = 2 * i
9+
if (2* i + 1) < length and alist[index] < alist[2 * i + 1]:
10+
index = index + 1
11+
if index != i:
12+
alist[i], alist[index] = alist[index], alist[i]
13+
ReBalance(alist, index)
14+
15+
16+
def GetHeap(alist):
17+
length = len(alist)
18+
start = length / 2 - 1
19+
20+
for i in range(start, 0, -1):
21+
ReBalance(alist, i)
22+
return alist
23+
24+
25+
if __name__ == '__main__':
26+
alist = [5, 13, 2, 25, 7, 17, 20, 8, 4]
27+
print GetHeap(alist)

0 commit comments

Comments
 (0)