Algorithms: Heap sort

Algorithms: Heap sort

The Heap sort algorithm is based on the idea of repeatedly extracting the largest or the smallest element from a heap and then reconstructing the heap.

But what is a heap? A heap is specialised binary tree-based data structure. heap property dictates the relationship between a parent node and its children.

We can split the heap into two. A Max-heap and a Min-heap.

  • Max-Heap: Every parent node is greater than or equal to its children. This means the largest element is at the root of the heap.
  • Min-Heap: Every parent node is less than or equal to its children. This means the smallest element is at the root of the heap.

A Heap is always a Complete Binary Tree. This means all levels of the tree are fully filled with possible exception of the last level, which is filled from left to right.

So why is a Complete Binary Tree required here?

  • For a Max-Heap and Min-Heap we need a structure where every parent node satisfies the heap property and relation and respect to its children.
  • With a Complete Binary Tree we ensure minimal height for efficient comparison and swapping.

Example Nodes: [1, 2, 3, 4, 5, 6].

Complete Binary Tree

Non Complete Binary Tree

Lets look at some code here for Heap sort.

Th

So now when we understand the basics of Heap sort and the complexity that comes with it we must practice this together.

Here is some basic -> Expert exercises for Heap Sort

Heap Sort Complexity

AlgorithmTime Complexity (Best)Time Complexity (Worst)Space ComplexityStable?
Heap SortO(n log n)O(n log n)O(1) (in-place)No

Comparison with other sorting algorithms

AlgorithmTime Complexity (Best)Time Complexity (Worst)Space ComplexityStable?
SelectionO(n2)O(n^2)O(n2)O(n^2)O(1)O(1)No
BubbleO(n)O(n) (optimized)O(n2)O(n^2)O(1)O(1)Yes
InsertionO(n)O(n)O(n2)O(n^2)O(1)O(1)Yes
MergeO(nlogn)O(n \log n)O(nlogn)O(n \log n)O(n)O(n)Yes
QuickO(nlogn)O(n \log n)O(n2)O(n^2)O(logn)O(\log n)No

So now when we understand the basics of Heap sort and the complexity that comes with it we must practice this together.

Here is some basic -> Expert exercises for Heap Sort

Algorithms: Insertion sort
Next algorithm I want to learn more about is the Insertion sort algorithm. This algorithm works like we humans sort playing cards by hand. This algorithm works by building a sorted portion of the array one element at a time. For each step in an array we do the following.
Algorithms: Quick Sort
So last time we learnt more about divide and conquer algorithms and Merge Sort. Lets continue down that road and look on an eficient sorting algorithm called Quick Sort. This algorithm at glance is simliar to Merge Sort but it works a little bit different. If you have not read