Heap Sort is an efficient sorting algorithm that leverages a data structure called Binary Heap to organize and sort data. It works by first building a heap from the data and then repeatedly extracting the largest (or smallest) element from the heap and rebuilding the heap until all elements are sorted. This method is known for its reliable performance and in-place sorting capabilities, making it a strong choice for handling large datasets without requiring extra memory.
function heapify(i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n:
if arr[left] > arr[largest]:
largest = left
if right < n:
if arr[right] > arr[largest]:
largest = right
if largest != i:
swap(i, largest)
heapify(largest)
for i = (n / 2 - 1) down to 0:
heapify(i)
for i = n - 1 down to 1:
swap(0, i)
heapify(0)
© 2025 SEE Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.