You are on page 1of 2

19CSE302 – DESIGN AND ANALYSIS OF

ALGORITHM

LAB 3 – HEAP SORT

CH SURYA UMA SHANKAR

CH.EN.U4CSE19101

CODE:
import time
st = time.time()
def buildmaxheap(a,n):
for i in range (n//2,-1,-1):
maxheapify(a,n,i)

def maxheapify(a,n,i):
largest=i
l=2*i+1
r=(2*i)+2
if l< n and a[l]>a[largest]:
largest=l
if r < n and a[r] > a[largest]:
largest=r
if largest != i :
a[i],a[largest]=a[largest],a[i]
#print(a)
maxheapify(a,n,largest)
def heapsort(a,n):
buildmaxheap(a,n)
for i in range(n-1,0,-1):
a[i],a[0]=a[0],a[i]
#maxheapify(a,i,0)
buildmaxheap(a,i)

a=[]
n=int(input("Enter no of elements:"))
for j in range (0,n):
a.append(int(input("enter value:")))
print(a)
heapsort(a,n)
print(a)
et = time.time()
print("Execution time: \n", et-st)

OUTPUT:

You might also like