Skip to content

Commit 38aae5d

Browse files
committed
Implement Heap Sort algorithm with heapify function and test cases
1 parent 3c4c397 commit 38aae5d

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
Heap Sort Algorithm
3+
Time Complexity: O(n log n) in all cases
4+
Space Complexity: O(1) - sorts in place
5+
Author: Karanjot Singh
6+
Date: October 2025
7+
Hacktoberfest 2025
8+
"""
9+
10+
def heapify(arr, n, i):
11+
"""
12+
Heapify subtree rooted at index i
13+
14+
Args:
15+
arr: Array to heapify
16+
n: Size of heap
17+
i: Root index of subtree
18+
"""
19+
largest = i
20+
left = 2 * i + 1
21+
right = 2 * i + 2
22+
23+
# Check if left child exists and is greater than root
24+
if left < n and arr[left] > arr[largest]:
25+
largest = left
26+
27+
# Check if right child exists and is greater than largest so far
28+
if right < n and arr[right] > arr[largest]:
29+
largest = right
30+
31+
# If largest is not root, swap and continue heapifying
32+
if largest != i:
33+
arr[i], arr[largest] = arr[largest], arr[i]
34+
heapify(arr, n, largest)
35+
36+
37+
def heap_sort(arr):
38+
"""
39+
Heap sort implementation
40+
41+
Steps:
42+
1. Build max heap from array
43+
2. Extract elements one by one from heap
44+
3. Place extracted element at the end
45+
46+
Args:
47+
arr: List to sort (modified in place)
48+
"""
49+
n = len(arr)
50+
51+
# Build max heap
52+
# Start from last non-leaf node and heapify each node
53+
for i in range(n // 2 - 1, -1, -1):
54+
heapify(arr, n, i)
55+
56+
# Extract elements from heap one by one
57+
for i in range(n - 1, 0, -1):
58+
# Move current root to end
59+
arr[0], arr[i] = arr[i], arr[0]
60+
61+
# Heapify the reduced heap
62+
heapify(arr, i, 0)
63+
64+
65+
def heap_sort_copy(arr):
66+
"""
67+
Heap sort that returns sorted copy
68+
69+
Args:
70+
arr: List to sort
71+
72+
Returns:
73+
Sorted copy of the list
74+
"""
75+
arr_copy = arr.copy()
76+
heap_sort(arr_copy)
77+
return arr_copy
78+
79+
80+
# Test cases
81+
if __name__ == "__main__":
82+
print("=== Heap Sort Algorithm ===\n")
83+
84+
# Test case 1: Random array
85+
test1 = [12, 11, 13, 5, 6, 7]
86+
print(f"Original array: {test1}")
87+
sorted1 = heap_sort_copy(test1)
88+
print(f"Sorted array: {sorted1}")
89+
90+
# Test case 2: Already sorted
91+
test2 = [1, 2, 3, 4, 5]
92+
print(f"\nOriginal array: {test2}")
93+
sorted2 = heap_sort_copy(test2)
94+
print(f"Sorted array: {sorted2}")
95+
96+
# Test case 3: Reverse sorted
97+
test3 = [10, 8, 6, 4, 2]
98+
print(f"\nOriginal array: {test3}")
99+
sorted3 = heap_sort_copy(test3)
100+
print(f"Sorted array: {sorted3}")
101+
102+
# Test case 4: Array with duplicates
103+
test4 = [4, 10, 3, 5, 1, 3, 10]
104+
print(f"\nOriginal array: {test4}")
105+
sorted4 = heap_sort_copy(test4)
106+
print(f"Sorted array: {sorted4}")
107+
108+
# Test case 5: Large array
109+
test5 = [64, 34, 25, 12, 22, 11, 90, 88, 45, 50, 23, 36]
110+
print(f"\nOriginal array: {test5}")
111+
sorted5 = heap_sort_copy(test5)
112+
print(f"Sorted array: {sorted5}")
113+
114+
# Test case 6: Single element
115+
test6 = [42]
116+
print(f"\nOriginal array: {test6}")
117+
sorted6 = heap_sort_copy(test6)
118+
print(f"Sorted array: {sorted6}")
119+
120+
print("\n✅ All test cases completed!")

0 commit comments

Comments
 (0)