-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.py
51 lines (39 loc) · 922 Bytes
/
heapsort.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 6 12:56:06 2016
@author: fang2
"""
def parent(i):
return i//2
def left(i):
return 2*i+1
def right(i):
return 2*i+2
def max_heapify(A,i,heap_size):
l=left(i)
r=right(i)
print(l)
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=len(A)
x=(heap_size//2)
for i in reversed(range(x)):
max_heapify(A,i,heap_size)
def heapsort(A):
build_max_heap(A)
heap_size=len(A)
for i in reversed(range(1,len(A))):
A[0],A[i]=A[i],A[0]
heap_size=heap_size-1
max_heapify(A,0,heap_size)
A=[2,3,1,4,6,5,8,7,9]
heapsort(A)
print (A)