diff --git a/Algorithms/Sort/InsertionSort/InsertionSort.cpp b/Algorithms/Sort/InsertionSort/InsertionSort.cpp new file mode 100644 index 0000000..d0e110a --- /dev/null +++ b/Algorithms/Sort/InsertionSort/InsertionSort.cpp @@ -0,0 +1,47 @@ +#include +#include + +/** + * @brief Perform Insertion Sort on a vector. + * + * This function sorts the elements of the vector in ascending order using the + * Insertion Sort algorithm. + * + * @param array Reference to the vector to be sorted. + */ +void insertionSort(std::vector& array) { + int arrayLength = array.size(); + + for(int i = 1; i < arrayLength; i++){ + int key = array[i]; + int j = i - 1; + + // Moving elements greater than key to one position ahed + while(j >= 0 && array[j] > key) { + array[j + 1] = array[j]; + j = j - 1; + } + + array[j + 1] = key; + } +} + +int main() { + std::vector exampleArray = {4, 1, 5, 8, 8, 10, 21, 0, -3, 2, 1, 3}; + + std::cout << "Original array: "; + for (int elem : exampleArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + insertionSort(exampleArray); + + std::cout << "Sorted array: "; + for (int elem : exampleArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + return 0; +}