insertion_sort_Rcpp | R Documentation |
This function implements the Insertion Sort algorithm in C++ and is exported to R using Rcpp. Insertion Sort is a simple sorting algorithm that builds the sorted sequence one element at a time by inserting each new element into its correct position within the sorted part of the vector.
The algorithm works by iterating through the input vector and shifting larger elements to the right before inserting the current element (key) into its proper position.
insertion_sort_Rcpp(v)
v |
A numeric vector containing unsorted elements. |
Time Complexity: O(n²) in the worst and average cases, O(n) in the best case (already sorted data).
Space Complexity: O(1) (in-place sorting, no additional memory used).
Stable Sorting Algorithm: Maintains the relative order of equal elements.
A numeric vector of the same length as 'v', sorted in ascending order.
insertion_sort_Rcpp(rnorm(100))
insertion_sort_Rcpp(sample(100))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.