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