TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Heap Sort Algorithm (heap sort) implementation More...
#include <algorithm>
#include <cassert>
#include <iostream>
Go to the source code of this file.
Functions | |
template<typename T > | |
void | printArray (T *arr, int sz) |
template<typename T > | |
void | heapify (T *arr, int n, int i) |
template<typename T > | |
void | heapSort (T *arr, int n) |
void | test () |
int | main () |
Heap Sort Algorithm (heap sort) implementation
Heap-sort is a comparison-based sorting algorithm. Heap-sort can be thought of as an improved selection sort: like selection sort, heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heap sort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.
Time Complexity - \(O(n \log(n))\)
Definition in file heap_sort.cpp.
int main | ( | void | ) |
Main function
Definition at line 120 of file heap_sort.cpp.
void printArray | ( | T * | arr, |
int | sz ) |
Utility function to print the array after sorting.
arr | array to be printed |
sz | size of array |
Definition at line 37 of file heap_sort.cpp.
void test | ( | ) |
Test cases to test the program
Definition at line 99 of file heap_sort.cpp.