heap_sort_Rcpp | R Documentation |
This function implements Heap Sort, an efficient sorting algorithm that first builds a max-heap and then repeatedly extracts the maximum element to produce a sorted sequence.
The algorithm consists of two main steps:
Heap Construction: The input vector is reorganized into a max-heap.
Sorting: The largest element (root) is swapped with the last element, reducing the heap size and reapplying heapify.
heap_sort_Rcpp(v)
v |
A numeric vector containing unsorted elements. |
Time Complexity:
Building the heap: O(n)
Extracting elements: O(n log n)
Overall: O(n log n)
Space Complexity: O(1) (in-place sorting).
Unstable Sort: The relative order of equal elements may change.
A numeric vector sorted in ascending order.
heap_sort_Rcpp(rnorm(100))
heap_sort_Rcpp(sample(100))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.