forked from gycg/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.py
More file actions
28 lines (25 loc) · 701 Bytes
/
heap_sort.py
File metadata and controls
28 lines (25 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def max_heapify(A, i, heap_size):
l = 2*i+1
r = 2*i+2
if l < heap_size and A[l] > A[i]:
largest = l
else:
largest = i
if r < heap_size and A[r] > A[largest]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest, heap_size)
def build_max_heap(A, heap_size):
for i in xrange(heap_size/2, -1, -1):
max_heapify(A, i, heap_size)
def heapsort(A):
heap_size = len(A)
build_max_heap(A, heap_size)
for i in xrange(len(A)-1, 0, -1):
A[0], A[i] = A[i], A[0]
heap_size -= 1
max_heapify(A, 0, heap_size)
A = [27,17,3,16,13,10,1,5,7,12,4,8,9,0]
heapsort(A)
print A