algorithm-notes
  • Preface
  • Outline
    • Asymptotic-Analysis
    • Divide-and-Conquer (DnC)
      • Overview
      • Multiplication
      • DnC Strategy
      • Master Method
    • Randomization
      • Overview
      • Discrete Probability
      • Randomized Algorithms
      • Reservoir Sampling
    • Sorting
      • Overview
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • Shell Sort
      • Merge Sort
      • Heap Sort
      • Quick Sort
      • Non-Comparison Sort
    • Graph Algorithms
      • Overview
      • Minimum Spanning Tree
      • Shortest Path
      • Topological Sort
    • Tree
      • Overview
      • Balanced Search Tree
        • AVL Tree
        • Red-Black Tree
        • B Tree
      • Heap
      • Disjoint Set
      • Threaded Binary Tree
      • Segment Tree
    • Searching
      • Overview
      • Binary Search
      • Graph Search
      • Hash Table
      • String Search
    • Dynamic Programming
      • Overview
      • Fibonacci Numbers
      • LCS problem
    • Greedy Algorithms
      • Overview
Powered by GitBook
On this page
  • Fundamental Ideas
  • Pseudocode
  • Algorithm Analysis
  • Additional References

Was this helpful?

  1. Outline
  2. Sorting

Heap Sort

PreviousMerge SortNextQuick Sort

Last updated 6 years ago

Was this helpful?

Based on a typical data structure, Heap Sort is an in-place, not stable comparison-baed sorting algorithm and can be thought of as a improved version of algorithm.

Fundamental Ideas

Initially, a is called upon a randomly distributed inputs to setup a . Now that the topmost root key must be the largest among the heap, extract that key to a sorted array and restore the heap property using operation. Repeat the procedures of extraction and adjustment till the heap is empty, the sorted array is formed.

In each heap restoration process, instead of method of locating the maximum or minimum key within the inputs in linear time, Heap Sort uses operation to reduce time complexity to logarithmic bound.

Pseudocode

The following code transforms a randomly distributed array into an ascending entry array.

HEAP_SORT(A)
  BUILD_MAX_HEAP(A)
  for i = length(A) to 2
    swap A[1] and A[i]
    A.heap_size = A.heap_size - 1
    MAX_HEAPIFY(A, i)

Algorithm Analysis

Given a n-size inputs, there is a Ο(n) estimate on operation; And for each input entry, a is expected to perform and thus costs Ο(n ⋅ log(n)) in total.

Since the is also a comparison-based sorting that has proven with a lower bound Ω(n ⋅ log(n)), the more tighter bound for Heap Sort will be Θ(n ⋅ log(n)).

Additional References

Why is Heap Sort used?

https://www.quora.com/Why-is-heap-sort-used
heap
selection sort
BUILD-MAX-HEAP
max-heap
MAX-HEAPIFY
selection sort
MAX-HEAPIFY
BUILD_MAX_HEAP
MAX_HEAPIFY
Heap Sort