-
Notifications
You must be signed in to change notification settings - Fork 186
/
HeapSort.cpp
37 lines (29 loc) · 1.06 KB
/
HeapSort.cpp
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
def max_heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
# Check if left child exists and is greater than the current largest
if left < n and arr[left] > arr[largest]:
largest = left
# Check if right child exists and is greater than the current largest
if right < n and arr[right] > arr[largest]:
largest = right
# If largest is not the root, swap and continue heapifying
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
max_heapify(arr, n, largest)
def heap_sort(arr):
n = len(arr)
# Build a max heap
for i in range(n // 2 - 1, -1, -1):
max_heapify(arr, n, i)
# One by one extract elements from the heap
for i in range(n - 1, 0, -1):
# Move current root to the end
arr[i], arr[0] = arr[0], arr[i] # Swap
max_heapify(arr, i, 0) # Call max heapify on the reduced heap
if __name__ == "__main__":
data = [int(x) for x in input("Enter elements separated by space: ").split()]
heap_sort(data)
print("Sorted Data:", data)
}