-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap sort.py
40 lines (40 loc) · 1.13 KB
/
heap sort.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
import random
from timeit import default_timer as timer
import matplotlib.pyplot as plt
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
for i in range(n // 2, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
x=[]
y=[]
for i in range(3):
n=int(input("\nEnter the value of n:"))
x.append(n)
arr = [random.randint(0, 10) for _ in range(n)]
print("Array elements before sorting are",arr)
start_time = timer()
ind=heapSort(arr)
end_time = timer()
elapsed_time = end_time - start_time
y.append(elapsed_time)
print("Array elements after sorting are ",arr)
print("Time taken=", elapsed_time)
plt.plot(x,y)
plt.title('Time Taken for heap sort')
plt.xlabel('n')
plt.ylabel('Time (seconds)')
plt.show()