0% found this document useful (0 votes)
187 views11 pages

Heap Sort Min-Heap or Max-Heap

Heap sort involves building a max heap from an array and then repeatedly extracting the maximum element from the heap and inserting it into the sorted end of the array. It does this by (1) building the max heap from the array in O(n) time, then (2) repeatedly removing the max element from the root of the heap and adding it to the sorted portion of the array, reheapifying each time in O(logn) time. Overall, heap sort runs in O(nlogn) time in all cases.

Uploaded by

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

Heap Sort Min-Heap or Max-Heap

Heap sort involves building a max heap from an array and then repeatedly extracting the maximum element from the heap and inserting it into the sorted end of the array. It does this by (1) building the max heap from the array in O(n) time, then (2) repeatedly removing the max element from the root of the heap and adding it to the sorted portion of the array, reheapifying each time in O(logn) time. Overall, heap sort runs in O(nlogn) time in all cases.

Uploaded by

Rehan Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Heap sort

 Heap sort processes the elements by creating the min-heap or max-heap


using the elements of the given array.

 Min-heap or max-heap represents the ordering of array in which each node


has the minimum or maximum element value than its children.

 Heap sort basically recursively performs two main operations -

1. Build a heap H, using the elements of array.

2. Repeatedly delete the root element of the heap formed in 1st phase.


What is a heap?
 A heap is a complete binary tree, and the binary tree is a tree in
which the node can have at most two children.
 A complete binary tree is a binary tree in which all the levels except
the last level, i.e., leaf node, should be completely filled, and all the
nodes should be left-justified.
 All nodes in the Heap tree follow the property that they are greater than
their children i.e. the largest element is at the root and both its children
are smaller than the root and so on. Such a heap is called a max-heap. If
instead, all nodes are smaller than their children, it is called a min-heap.
What is heap sort?

 Heapsort is a popular and efficient sorting algorithm. The concept of


heap sort is to eliminate the elements one by one from the heap part of
the array, and then insert them into the sorted part of the array.
 Heapsort is the in-place sorting algorithm.
Relationship between Array Indexes and Tree Elements
 A complete binary tree has an interesting property that we can use to find
the children and parents of any node.
 If the index of any element in the array is i, the element in the index 2i+1
 will become the left child and element in 2i+2 index will become the right child.
 Also, the parent of any element at index i is given by the lower bound of (i-1)/2.
How to "heapify" a tree ?

 Starting from a complete binary tree, we can modify it to become a Max-Heap by


running a function called heapify on all the non-leaf elements of the heap.

 Since heappify uses recursion, it can be difficult to grasp. So let's first think about
how you would heapify a tree with just three elements.

heapify(array)
Root = array[0]
Largest = max( array[0] , array [2*0 + 1], array[2*0+2])
if(Root != Largest)
Swap(Root, Largest)
How to heapify root element when its subtrees are already max heaps ?
 The top element isn't a max-heap but all the sub-trees are max-heaps.
 To maintain the max-heap property for the entire tree, we will have to keep pushing 2 downwards until it reaches its
correct position.

(a) Heap

Thus, to maintain the max-heap property in


a tree where both sub-trees are max-heaps,
we need to run heapify on the root element (b) Converting Heap to max-heap using heapify function
repeatedly until it is larger than its children
or it becomes a leaf node.
void heapify(int arr[], int n, int i)
{
// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

// Swap and continue heapifying if root is not largest


if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
Build max-heap
 To build a max-heap from any tree, we can thus start heapifying
each sub-tree from the bottom up and end up with a max-heap
after the function is applied to all the elements including the root
element.

 In the case of a complete tree, the first index of a non-leaf node is


given by n/2 - 1.
 All other nodes after that are leaf-nodes and thus don't need to be
heapified.

 So, we can build a maximum heap as

// Build heap (rearrange array)


for (int i = n / 2 - 1; i >= 0; i--)
heapify( arr, n, i);
Working of Heap Sort

1.Since the tree satisfies Max-Heap property, then the largest element
is stored at the root node.

2.Swap: Remove the root element and put at the end of the array
(at nth position) Put the last element of the tree (heap) at the vacant
place.

3.Remove: Reduce the size of the heap by 1.

4.Heapify: Heapify the root element again so that we have the largest
element at root.

5.The process is repeated until all the items of the list are sorted.
•Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of heap sort is O(n logn).

•Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of heap sort is O(n log n).

•Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array elements
in ascending order, but its elements are in descending order. The worst-case
time complexity of heap sort is O(n log n).

The time complexity of heap sort is O(n logn) in all three cases (best case,
average case, and worst case). The height of a complete binary tree having n
elements is logn.

You might also like