-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.py
More file actions
55 lines (49 loc) · 1.18 KB
/
heapsort.py
File metadata and controls
55 lines (49 loc) · 1.18 KB
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
52
53
54
55
import random
import math
def heapsort(arr):
global heap
heap=['-']
heapify(arr)
sorted_arr=[]
while(len(heap)>1):
sorted_arr.append(removeMax())
sorted_arr.reverse()
for i in range(len(arr)):
arr[i]=sorted_arr[i]
def heapify(arr):
for i in range(len(arr)):
insert(arr[i])
def removeMax():
last=len(heap)-1
max_val = heap[1]
heap[1]=heap[last]
heap.pop(-1)
parent=1
left=2*parent
right=2*parent+1
while (left<len(heap) and heap[parent]<heap[left]) or (right<len(heap) and heap[parent]<heap[right]):
if right<len(heap) and heap[left]<heap[right]:
swap(parent,right)
parent=right
else:
swap(parent,left)
parent=left
left=2*parent
right=2*parent+1
return max_val
def insert(element):
heap.append(element)
n=len(heap)-1 #last index
parent=math.floor(n/2)
while parent>=1 and heap[parent]<heap[n]:
swap(parent,n)
n=parent
parent=math.floor(n/2)
def swap(i,j):
temp=heap[i]
heap[i]=heap[j]
heap[j]=temp
arr=[random.randint(1,100) for i in range(10)]
print(arr)
heapsort(arr)
print(arr)