Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Latest commit

 

History

History
54 lines (32 loc) · 1.52 KB

Heap-Sort.md

File metadata and controls

54 lines (32 loc) · 1.52 KB

Heap Sort

  1. Time Complexity: O(log(n)) due to heapify(logn) and CreateandBuildHeap is O(n).
  2. Space Complexity: O(1).
  3. Applications: Finding the order in statistics, dealing with priority queues in Prim's algorithm and Huffman encoding or data compression.
  4. Founder's Name: John William Joseph Williams

Steps

  1. Create and build a max heap.
  2. Remove the root element and put at the end of the list. Put the last item of the tree at the vacant place.
  3. Reduce the size of the heap by 1.
  4. Heapify the root element again so that we have the highest element at root.
  5. Repeat this process until all the elements are sorted.

Example

Given array : [4, 10, 3, 5, 1]

1. Create a heap: [4, 10, 3, 5, 1]

2. Build a max heap: [10, 5, 3, 4, 1]

3. Remove the node: [1, 5, 3, 4, 10]

4. Build a max heap: [5, 4, 3, 1, 10]

5. Remove the node: [1, 4, 3, 5, 10]

6. Build a max heap: [4, 1, 3, 5, 10]

7. Remove the node: [3, 1, 4, 5, 10]

8. Already max heap: [3, 1, 4, 5, 10]

9. Remove the node: [1, 3, 4, 5, 10]

10. Sorted Array: [1, 3, 4, 5, 10]

Implementation

Video URL

Youtube Video about Heap Sort

Others

Wikipedia