From 02ee7e19642569f3c45a1fbae3e2c36ed95186db Mon Sep 17 00:00:00 2001 From: harshitha-85 <57164562+harshitha-85@users.noreply.github.com> Date: Wed, 30 Oct 2019 10:51:51 +0530 Subject: [PATCH] Implementation of Heap-sort The code also computes the number of times heapify() and heapsort() are called. P.S. - Enter the space-separated array. --- heapsort.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 heapsort.py diff --git a/heapsort.py b/heapsort.py new file mode 100644 index 0000000..5745409 --- /dev/null +++ b/heapsort.py @@ -0,0 +1,42 @@ +res = [] +cs = ch =sw= 0 + +def heapsort(heap): + global res,cs + cs+=1 + if len(heap)>1: + n = (len(heap)//2)-1 + for i in range(n,-1,-1): + heapify(heap,i) + res.append(heap[0]) + heap[0] = heap[len(heap)-1] + heap.pop() + heapsort(heap) + else: + res.append(heap[0]) + +def heapify(heap,i): + global ch,sw + ch+=1 + small = i + l = 2*i+1 + r = 2*i+2 + if l