0% found this document useful (0 votes)
2 views1 page

Heap Sort Algorithm

The document outlines the Heap Sort algorithm, which involves building a max heap from an array and then sorting it by repeatedly swapping the root with the last element and heapifying the reduced heap. It provides pseudocode for both the HeapSort and Heapify functions. The algorithm ensures that the largest elements are sorted in descending order.

Uploaded by

sp1251197
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Heap Sort Algorithm

The document outlines the Heap Sort algorithm, which involves building a max heap from an array and then sorting it by repeatedly swapping the root with the last element and heapifying the reduced heap. It provides pseudocode for both the HeapSort and Heapify functions. The algorithm ensures that the largest elements are sorted in descending order.

Uploaded by

sp1251197
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Heap Sort Algorithm

Pseudocode:
HeapSort(arr):
BuildMaxHeap(arr)

for i = length(arr)-1 down to 1:


swap(arr[0], arr[i])
Heapify(arr, 0, i)

Heapify(arr, root, size):


largest = root
left = 2 * root + 1
right = 2 * root + 2

if left < size and arr[left] > arr[largest]:


largest = left

if right < size and arr[right] > arr[largest]:


largest = right

if largest != root:
swap(arr[root], arr[largest])
Heapify(arr, largest, size)

You might also like