Skip to content

Commit

Permalink
Merge pull request #631 from vaibhav0726/patch-1
Browse files Browse the repository at this point in the history
added heap sort algo in python
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents 12a26d0 + ef619bd commit 54f3455
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Python/Sorting-Techniques/heapSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def heapify(arr, N, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2

if l < N and arr[largest] < 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, -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)

if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]

heapSort(arr)
N = len(arr)

print("Sorted array is")
for i in range(N):
print("%d" % arr[i], end=" ")

0 comments on commit 54f3455

Please sign in to comment.