From f7f348ff811d07585b83caf57b6c1430a24007d7 Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Mon, 22 Jan 2024 20:24:51 +0530 Subject: [PATCH 1/2] Added C++ code for Insertion Sort Algorithm --- .../Sort/InsertionSort/InsertionSort.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Algorithms/Sort/InsertionSort/InsertionSort.cpp diff --git a/Algorithms/Sort/InsertionSort/InsertionSort.cpp b/Algorithms/Sort/InsertionSort/InsertionSort.cpp new file mode 100644 index 0000000..3c3f9e7 --- /dev/null +++ b/Algorithms/Sort/InsertionSort/InsertionSort.cpp @@ -0,0 +1,53 @@ +#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) { + // This is the length of given array + int arrayLength = array.size(); + + // Iterating through each element of the array from index 1 to lastIndex(arrayLength -1) + 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() { + // Example usage + std::vector myArray = {4, 1, 5, 8, 8, 10, 21, 0, -3, 2, 1, 3}; + + // Print the original array + std::cout << "Original array: "; + for (int elem : myArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + // Sort the array using Insertion Sort + insertionSort(myArray); + + // Print the sorted array + std::cout << "Sorted array: "; + for (int elem : myArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + return 0; +} From cc6e82b745b88532d4a7f331dbc31c1487c8d580 Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Sun, 28 Jan 2024 18:33:06 +0530 Subject: [PATCH 2/2] InsertionSort: Removed unwanted comments. --- Algorithms/Sort/InsertionSort/InsertionSort.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Algorithms/Sort/InsertionSort/InsertionSort.cpp b/Algorithms/Sort/InsertionSort/InsertionSort.cpp index 3c3f9e7..d0e110a 100644 --- a/Algorithms/Sort/InsertionSort/InsertionSort.cpp +++ b/Algorithms/Sort/InsertionSort/InsertionSort.cpp @@ -10,10 +10,8 @@ * @param array Reference to the vector to be sorted. */ void insertionSort(std::vector& array) { - // This is the length of given array int arrayLength = array.size(); - // Iterating through each element of the array from index 1 to lastIndex(arrayLength -1) for(int i = 1; i < arrayLength; i++){ int key = array[i]; int j = i - 1; @@ -29,22 +27,18 @@ void insertionSort(std::vector& array) { } int main() { - // Example usage - std::vector myArray = {4, 1, 5, 8, 8, 10, 21, 0, -3, 2, 1, 3}; + std::vector exampleArray = {4, 1, 5, 8, 8, 10, 21, 0, -3, 2, 1, 3}; - // Print the original array std::cout << "Original array: "; - for (int elem : myArray) { + for (int elem : exampleArray) { std::cout << elem << " "; } std::cout << std::endl; - // Sort the array using Insertion Sort - insertionSort(myArray); + insertionSort(exampleArray); - // Print the sorted array std::cout << "Sorted array: "; - for (int elem : myArray) { + for (int elem : exampleArray) { std::cout << elem << " "; } std::cout << std::endl;