maTH
  • 👋Welcome!
  • 🗿Installation
  • Reference
    • 😱Mathematical operations
      • add
      • pascal
      • factorial
      • factorialL
      • GCD
      • LCM
      • pow
      • permutations
      • combinations
      • combinationsWithRep
      • subtract
      • multiply
      • divide
      • sin
      • cos
      • ln
      • log
      • radToDeg
      • degToRad
      • mod
      • abs
      • floor
      • ceil
      • root
      • exp
      • tan
      • arcsin
      • arccos
      • arctan
      • sinh
      • cosh
      • round
    • 🥶Statistics
      • Binomial
      • Poisson
      • Geometric
      • Pascal
      • Hypergeometric
      • Exponential
      • Uniform
    • 🥵Data structures
      • Stack
      • Queue
      • LinkedList
    • 🤓Algorithms
      • Bubble sort
      • Insertion sort
      • Selection sort
      • Merge sort
      • Quicksort
      • Heap sort
      • Count sort
      • Bucket sort
      • Radix sort
    • 😳Constants
Powered by GitBook
On this page
  • Sort a vector using heap sort:
  • Example use case:
  1. Reference
  2. Algorithms

Heap sort

Sort a vector using heap sort:

heapSort(arr, isAscending);

returns the sorted vector arr, based on provided isAscending condition (true for ascending and false for descending).

Example use case:

Running

std::vector<int> arr= {5, 2, 9, 1, 5, 6};

std::cout << "Original Vector: ";
for(int num : arr){
    std::cout << num << " ";
}    
std::cout << std::endl;

heapSort(arr, true);

std::cout << "Sorted Ascending Vector: ";
for(int num : arr){
    std::cout << num << " ";
}
std::cout << std::endl;

, returns the following output:

Original Vector: 5 2 9 1 5 6 
Sorted Ascending Vector: 1 2 5 5 6 9 

PreviousQuicksortNextCount sort

Last updated 2 months ago

🤓