From a3834f7bfbdda8d1925b4a7dccc0527d4266a73e Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:19:33 +0530 Subject: [PATCH 01/12] added recursive insertion sort algorithm --- insertion_sort_recursive.cpp | 152 +++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 insertion_sort_recursive.cpp diff --git a/insertion_sort_recursive.cpp b/insertion_sort_recursive.cpp new file mode 100644 index 00000000000..f303bb5ee8f --- /dev/null +++ b/insertion_sort_recursive.cpp @@ -0,0 +1,152 @@ +/** + * \file + * \brief [Recursive Insertion Sort Algorithm + * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) + * + * \details + * Insertion sort is a simple sorting algorithm that builds the final + * sorted array one at a time. The recursive version applies the same + * logic but sorts the sub-array recursively before inserting the current + * element in its correct position. + */ + +#include +#include +#include +#include + +/** \namespace sorting + * \brief Sorting algorithms + */ +namespace sorting { +/** \brief + * Recursive Insertion Sort Function + * + * @tparam T type of array + * @param [in,out] arr Array to be sorted + * @param n Size of Array + */ +template +void recursiveInsertionSort(T *arr, int n) { + // Base case: Array of size 1 is already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted array + T last = arr[n - 1]; + int j = n - 2; + + // Move elements of arr[0..n-1] that are greater than last to one + // position ahead of their current position + while (j >= 0 && arr[j] > last) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = last; +} + +/** Recursive Insertion Sort Function for a vector + * + * @tparam T type of vector elements + * @param [in,out] arr pointer to vector to be sorted + */ +template +void recursiveInsertionSort(std::vector *arr, size_t n) { + // Base case: If the vector has one or fewer elements, it's already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted vector + T last = arr->at(n - 1); + int j = n - 2; + + while (j >= 0 && arr->at(j) > last) { + arr->at(j + 1) = arr->at(j); + j--; + } + arr->at(j + 1) = last; +} + +} // namespace sorting + +/** Test Cases to test recursive algorithm */ +void tests() { + int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; + std::cout << "Test 1... "; + sorting::recursiveInsertionSort(arr1, 10); + assert(std::is_sorted(arr1, arr1 + 10)); + std::cout << "passed" << std::endl; + + int arr2[5] = {5, -3, 7, -2, 1}; + std::cout << "Test 2... "; + sorting::recursiveInsertionSort(arr2, 5); + assert(std::is_sorted(arr2, arr2 + 5)); + std::cout << "passed" << std::endl; + + float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; + std::cout << "Test 3... "; + sorting::recursiveInsertionSort(arr3, 5); + assert(std::is_sorted(arr3, arr3 + 5)); + std::cout << "passed" << std::endl; + + std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); + std::cout << "Test 4... "; + sorting::recursiveInsertionSort(&arr4, arr4.size()); + assert(std::is_sorted(std::begin(arr4), std::end(arr4))); + std::cout << "passed" << std::endl; + + int arr5[50]; + std::cout << "Test 5... "; + create_random_array(arr5, 50); + sorting::recursiveInsertionSort(arr5, 50); + assert(std::is_sorted(arr5, arr5 + 50)); + std::cout << "passed" << std::endl; + + float arr6[50]; + std::cout << "Test 6... "; + create_random_array(arr6, 50); + sorting::recursiveInsertionSort(arr6, 50); + assert(std::is_sorted(arr6, arr6 + 50)); + std::cout << "passed" << std::endl; +} + +/** Main Function */ +int main() { + /// Running predefined tests to test recursive algorithm + tests(); + + /// For user interaction + size_t n; + std::cout << "Enter the length of your array (0 to exit): "; + std::cin >> n; + if (n == 0) { + return 0; + } + + int *arr = new int[n]; + std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; + + for (int i = 0; i < n; i++) { + std::cin >> arr[i]; + } + + sorting::recursiveInsertionSort(arr, n); + + std::cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) { + std::cout << arr[i] << " "; + } + + std::cout << std::endl; + delete[] arr; + return 0; +} + From 1053c8dee09536144baf6f4c25d987ae7a5e3936 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 17:11:04 +0530 Subject: [PATCH 02/12] Modified the main function and removed user interaction as requested --- sorting/insertion_sort.cpp | 179 ++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 104 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index c9bac4bf704..fd7657c78e8 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -1,101 +1,90 @@ /** - * * \file - * \brief [Insertion Sort Algorithm + * \brief [Recursive Insertion Sort Algorithm * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) * * \details * Insertion sort is a simple sorting algorithm that builds the final - * sorted array one at a time. It is much less efficient compared to - * other sorting algorithms like heap sort, merge sort or quick sort. - * However it has several advantages such as - * 1. Easy to implement - * 2. For small set of data it is quite efficient - * 3. More efficient that other Quadratic complexity algorithms like - * Selection sort or bubble sort. - * 4. It's stable that is it does not change the relative order of - * elements with equal keys - * 5. Works on hand means it can sort the array or list as it receives. - * - * It is based on the same idea that people use to sort the playing cards in - * their hands. - * the algorithms goes in the manner that we start iterating over the array - * of elements as soon as we find a unsorted element that is a misplaced - * element we place it at a sorted position. - * - * Example execution steps: - * 1. Suppose initially we have - * \f{bmatrix}{4 &3 &2 &5 &1\f} - * 2. We start traversing from 4 till we reach 1 - * when we reach at 3 we find that it is misplaced so we take 3 and place - * it at a correct position thus the array will become - * \f{bmatrix}{3 &4 &2 &5 &1\f} - * 3. In the next iteration we are at 2 we find that this is also misplaced so - * we place it at the correct sorted position thus the array in this iteration - * becomes - * \f{bmatrix}{2 &3 &4 &5 &1\f} - * 4. We do not do anything with 5 and move on to the next iteration and - * select 1 which is misplaced and place it at correct position. Thus, we have - * \f{bmatrix}{1 &2 &3 &4 &5\f} + * sorted array one at a time. The recursive version applies the same + * logic but sorts the sub-array recursively before inserting the current + * element in its correct position. */ -#include -#include -#include -#include +#include // for std::is_sorted and std::swap +#include // for assert +#include // for std::cout and std::endl +#include // for std::vector /** \namespace sorting * \brief Sorting algorithms */ namespace sorting { -/** \brief - * Insertion Sort Function +/** + * \brief Recursive Insertion Sort Function for an array * - * @tparam T type of array - * @param [in,out] arr Array to be sorted - * @param n Size of Array + * @tparam T Type of the elements in the array + * @param[in,out] arr Pointer to the array to be sorted + * @param[in] n Size of the array */ template -void insertionSort(T *arr, int n) { - for (int i = 1; i < n; i++) { - T temp = arr[i]; - int j = i - 1; - while (j >= 0 && temp < arr[j]) { - arr[j + 1] = arr[j]; - j--; - } - arr[j + 1] = temp; +void recursiveInsertionSort(T *arr, int n) { + // Base case: Array of size 1 is already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted array + T last = arr[n - 1]; + int j = n - 2; + + // Move elements of arr[0..n-1] that are greater than last to one + // position ahead of their current position + while (j >= 0 && arr[j] > last) { + arr[j + 1] = arr[j]; + j--; } + arr[j + 1] = last; } -/** Insertion Sort Function +/** + * \brief Recursive Insertion Sort Function for a vector * - * @tparam T type of array - * @param [in,out] arr pointer to array to be sorted + * @tparam T Type of the elements in the vector + * @param[in,out] arr Pointer to the vector to be sorted + * @param[in] n Size of the vector */ template -void insertionSort(std::vector *arr) { - size_t n = arr->size(); - - for (size_t i = 1; i < n; i++) { - T temp = arr[0][i]; - int32_t j = i - 1; - while (j >= 0 && temp < arr[0][j]) { - arr[0][j + 1] = arr[0][j]; - j--; - } - arr[0][j + 1] = temp; +void recursiveInsertionSort(std::vector *arr, size_t n) { + // Base case: If the vector has one or fewer elements, it's already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted vector + T last = arr->at(n - 1); + int j = n - 2; + + while (j >= 0 && arr->at(j) > last) { + arr->at(j + 1) = arr->at(j); + j--; } + arr->at(j + 1) = last; } } // namespace sorting -/** - * @brief Create a random array objecthelper function to create a random array +/** + * \brief Helper function to create a random array * - * @tparam T type of array - * @param arr array to fill (must be pre-allocated) - * @param N number of array elements + * @tparam T Type of array elements + * @param[out] arr Pointer to the array to be filled + * @param[in] N Size of the array */ template static void create_random_array(T *arr, int N) { @@ -105,75 +94,57 @@ static void create_random_array(T *arr, int N) { } } -/** Test Cases to test algorithm */ +/** + * \brief Test cases to validate the recursive insertion sort algorithm + */ void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; std::cout << "Test 1... "; - sorting::insertionSort(arr1, 10); + sorting::recursiveInsertionSort(arr1, 10); assert(std::is_sorted(arr1, arr1 + 10)); std::cout << "passed" << std::endl; int arr2[5] = {5, -3, 7, -2, 1}; std::cout << "Test 2... "; - sorting::insertionSort(arr2, 5); + sorting::recursiveInsertionSort(arr2, 5); assert(std::is_sorted(arr2, arr2 + 5)); std::cout << "passed" << std::endl; float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; std::cout << "Test 3... "; - sorting::insertionSort(arr3, 5); + sorting::recursiveInsertionSort(arr3, 5); assert(std::is_sorted(arr3, arr3 + 5)); std::cout << "passed" << std::endl; std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); std::cout << "Test 4... "; - sorting::insertionSort(&arr4); + sorting::recursiveInsertionSort(&arr4, arr4.size()); assert(std::is_sorted(std::begin(arr4), std::end(arr4))); std::cout << "passed" << std::endl; int arr5[50]; std::cout << "Test 5... "; create_random_array(arr5, 50); - sorting::insertionSort(arr5, 50); + sorting::recursiveInsertionSort(arr5, 50); assert(std::is_sorted(arr5, arr5 + 50)); std::cout << "passed" << std::endl; float arr6[50]; std::cout << "Test 6... "; create_random_array(arr6, 50); - sorting::insertionSort(arr6, 50); + sorting::recursiveInsertionSort(arr6, 50); assert(std::is_sorted(arr6, arr6 + 50)); std::cout << "passed" << std::endl; } -/** Main Function */ +/** + * \brief Main function + * + * Empty except for the call to `tests()`. + * + * @return 0 Always returns 0. + */ int main() { - /// Running predefined tests to test algorithm tests(); - - /// For user insteraction - size_t n; - std::cout << "Enter the length of your array (0 to exit): "; - std::cin >> n; - if (n == 0) { - return 0; - } - - int *arr = new int[n]; - std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; - - for (int i = 0; i < n; i++) { - std::cin >> arr[i]; - } - - sorting::insertionSort(arr, n); - - std::cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) { - std::cout << arr[i] << " "; - } - - std::cout << std::endl; - delete[] arr; return 0; } From 73892b1b38416c518030ef29f13272905688b7d0 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:34:05 +0530 Subject: [PATCH 03/12] Deleted previous file --- insertion_sort_recursive.cpp | 152 ----------------------------------- 1 file changed, 152 deletions(-) delete mode 100644 insertion_sort_recursive.cpp diff --git a/insertion_sort_recursive.cpp b/insertion_sort_recursive.cpp deleted file mode 100644 index f303bb5ee8f..00000000000 --- a/insertion_sort_recursive.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/** - * \file - * \brief [Recursive Insertion Sort Algorithm - * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) - * - * \details - * Insertion sort is a simple sorting algorithm that builds the final - * sorted array one at a time. The recursive version applies the same - * logic but sorts the sub-array recursively before inserting the current - * element in its correct position. - */ - -#include -#include -#include -#include - -/** \namespace sorting - * \brief Sorting algorithms - */ -namespace sorting { -/** \brief - * Recursive Insertion Sort Function - * - * @tparam T type of array - * @param [in,out] arr Array to be sorted - * @param n Size of Array - */ -template -void recursiveInsertionSort(T *arr, int n) { - // Base case: Array of size 1 is already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted array - T last = arr[n - 1]; - int j = n - 2; - - // Move elements of arr[0..n-1] that are greater than last to one - // position ahead of their current position - while (j >= 0 && arr[j] > last) { - arr[j + 1] = arr[j]; - j--; - } - arr[j + 1] = last; -} - -/** Recursive Insertion Sort Function for a vector - * - * @tparam T type of vector elements - * @param [in,out] arr pointer to vector to be sorted - */ -template -void recursiveInsertionSort(std::vector *arr, size_t n) { - // Base case: If the vector has one or fewer elements, it's already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted vector - T last = arr->at(n - 1); - int j = n - 2; - - while (j >= 0 && arr->at(j) > last) { - arr->at(j + 1) = arr->at(j); - j--; - } - arr->at(j + 1) = last; -} - -} // namespace sorting - -/** Test Cases to test recursive algorithm */ -void tests() { - int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; - std::cout << "Test 1... "; - sorting::recursiveInsertionSort(arr1, 10); - assert(std::is_sorted(arr1, arr1 + 10)); - std::cout << "passed" << std::endl; - - int arr2[5] = {5, -3, 7, -2, 1}; - std::cout << "Test 2... "; - sorting::recursiveInsertionSort(arr2, 5); - assert(std::is_sorted(arr2, arr2 + 5)); - std::cout << "passed" << std::endl; - - float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; - std::cout << "Test 3... "; - sorting::recursiveInsertionSort(arr3, 5); - assert(std::is_sorted(arr3, arr3 + 5)); - std::cout << "passed" << std::endl; - - std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); - std::cout << "Test 4... "; - sorting::recursiveInsertionSort(&arr4, arr4.size()); - assert(std::is_sorted(std::begin(arr4), std::end(arr4))); - std::cout << "passed" << std::endl; - - int arr5[50]; - std::cout << "Test 5... "; - create_random_array(arr5, 50); - sorting::recursiveInsertionSort(arr5, 50); - assert(std::is_sorted(arr5, arr5 + 50)); - std::cout << "passed" << std::endl; - - float arr6[50]; - std::cout << "Test 6... "; - create_random_array(arr6, 50); - sorting::recursiveInsertionSort(arr6, 50); - assert(std::is_sorted(arr6, arr6 + 50)); - std::cout << "passed" << std::endl; -} - -/** Main Function */ -int main() { - /// Running predefined tests to test recursive algorithm - tests(); - - /// For user interaction - size_t n; - std::cout << "Enter the length of your array (0 to exit): "; - std::cin >> n; - if (n == 0) { - return 0; - } - - int *arr = new int[n]; - std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; - - for (int i = 0; i < n; i++) { - std::cin >> arr[i]; - } - - sorting::recursiveInsertionSort(arr, n); - - std::cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) { - std::cout << arr[i] << " "; - } - - std::cout << std::endl; - delete[] arr; - return 0; -} - From 2acae01844c171e508052a8d72202db622306cdc Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:03:31 +0530 Subject: [PATCH 04/12] Updated insertion_sort.cpp to its original code --- sorting/insertion_sort.cpp | 179 +++++++++++++++++++++---------------- 1 file changed, 104 insertions(+), 75 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index fd7657c78e8..c9bac4bf704 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -1,90 +1,101 @@ /** + * * \file - * \brief [Recursive Insertion Sort Algorithm + * \brief [Insertion Sort Algorithm * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) * * \details * Insertion sort is a simple sorting algorithm that builds the final - * sorted array one at a time. The recursive version applies the same - * logic but sorts the sub-array recursively before inserting the current - * element in its correct position. + * sorted array one at a time. It is much less efficient compared to + * other sorting algorithms like heap sort, merge sort or quick sort. + * However it has several advantages such as + * 1. Easy to implement + * 2. For small set of data it is quite efficient + * 3. More efficient that other Quadratic complexity algorithms like + * Selection sort or bubble sort. + * 4. It's stable that is it does not change the relative order of + * elements with equal keys + * 5. Works on hand means it can sort the array or list as it receives. + * + * It is based on the same idea that people use to sort the playing cards in + * their hands. + * the algorithms goes in the manner that we start iterating over the array + * of elements as soon as we find a unsorted element that is a misplaced + * element we place it at a sorted position. + * + * Example execution steps: + * 1. Suppose initially we have + * \f{bmatrix}{4 &3 &2 &5 &1\f} + * 2. We start traversing from 4 till we reach 1 + * when we reach at 3 we find that it is misplaced so we take 3 and place + * it at a correct position thus the array will become + * \f{bmatrix}{3 &4 &2 &5 &1\f} + * 3. In the next iteration we are at 2 we find that this is also misplaced so + * we place it at the correct sorted position thus the array in this iteration + * becomes + * \f{bmatrix}{2 &3 &4 &5 &1\f} + * 4. We do not do anything with 5 and move on to the next iteration and + * select 1 which is misplaced and place it at correct position. Thus, we have + * \f{bmatrix}{1 &2 &3 &4 &5\f} */ -#include // for std::is_sorted and std::swap -#include // for assert -#include // for std::cout and std::endl -#include // for std::vector +#include +#include +#include +#include /** \namespace sorting * \brief Sorting algorithms */ namespace sorting { -/** - * \brief Recursive Insertion Sort Function for an array +/** \brief + * Insertion Sort Function * - * @tparam T Type of the elements in the array - * @param[in,out] arr Pointer to the array to be sorted - * @param[in] n Size of the array + * @tparam T type of array + * @param [in,out] arr Array to be sorted + * @param n Size of Array */ template -void recursiveInsertionSort(T *arr, int n) { - // Base case: Array of size 1 is already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted array - T last = arr[n - 1]; - int j = n - 2; - - // Move elements of arr[0..n-1] that are greater than last to one - // position ahead of their current position - while (j >= 0 && arr[j] > last) { - arr[j + 1] = arr[j]; - j--; +void insertionSort(T *arr, int n) { + for (int i = 1; i < n; i++) { + T temp = arr[i]; + int j = i - 1; + while (j >= 0 && temp < arr[j]) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = temp; } - arr[j + 1] = last; } -/** - * \brief Recursive Insertion Sort Function for a vector +/** Insertion Sort Function * - * @tparam T Type of the elements in the vector - * @param[in,out] arr Pointer to the vector to be sorted - * @param[in] n Size of the vector + * @tparam T type of array + * @param [in,out] arr pointer to array to be sorted */ template -void recursiveInsertionSort(std::vector *arr, size_t n) { - // Base case: If the vector has one or fewer elements, it's already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted vector - T last = arr->at(n - 1); - int j = n - 2; - - while (j >= 0 && arr->at(j) > last) { - arr->at(j + 1) = arr->at(j); - j--; +void insertionSort(std::vector *arr) { + size_t n = arr->size(); + + for (size_t i = 1; i < n; i++) { + T temp = arr[0][i]; + int32_t j = i - 1; + while (j >= 0 && temp < arr[0][j]) { + arr[0][j + 1] = arr[0][j]; + j--; + } + arr[0][j + 1] = temp; } - arr->at(j + 1) = last; } } // namespace sorting -/** - * \brief Helper function to create a random array +/** + * @brief Create a random array objecthelper function to create a random array * - * @tparam T Type of array elements - * @param[out] arr Pointer to the array to be filled - * @param[in] N Size of the array + * @tparam T type of array + * @param arr array to fill (must be pre-allocated) + * @param N number of array elements */ template static void create_random_array(T *arr, int N) { @@ -94,57 +105,75 @@ static void create_random_array(T *arr, int N) { } } -/** - * \brief Test cases to validate the recursive insertion sort algorithm - */ +/** Test Cases to test algorithm */ void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; std::cout << "Test 1... "; - sorting::recursiveInsertionSort(arr1, 10); + sorting::insertionSort(arr1, 10); assert(std::is_sorted(arr1, arr1 + 10)); std::cout << "passed" << std::endl; int arr2[5] = {5, -3, 7, -2, 1}; std::cout << "Test 2... "; - sorting::recursiveInsertionSort(arr2, 5); + sorting::insertionSort(arr2, 5); assert(std::is_sorted(arr2, arr2 + 5)); std::cout << "passed" << std::endl; float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; std::cout << "Test 3... "; - sorting::recursiveInsertionSort(arr3, 5); + sorting::insertionSort(arr3, 5); assert(std::is_sorted(arr3, arr3 + 5)); std::cout << "passed" << std::endl; std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); std::cout << "Test 4... "; - sorting::recursiveInsertionSort(&arr4, arr4.size()); + sorting::insertionSort(&arr4); assert(std::is_sorted(std::begin(arr4), std::end(arr4))); std::cout << "passed" << std::endl; int arr5[50]; std::cout << "Test 5... "; create_random_array(arr5, 50); - sorting::recursiveInsertionSort(arr5, 50); + sorting::insertionSort(arr5, 50); assert(std::is_sorted(arr5, arr5 + 50)); std::cout << "passed" << std::endl; float arr6[50]; std::cout << "Test 6... "; create_random_array(arr6, 50); - sorting::recursiveInsertionSort(arr6, 50); + sorting::insertionSort(arr6, 50); assert(std::is_sorted(arr6, arr6 + 50)); std::cout << "passed" << std::endl; } -/** - * \brief Main function - * - * Empty except for the call to `tests()`. - * - * @return 0 Always returns 0. - */ +/** Main Function */ int main() { + /// Running predefined tests to test algorithm tests(); + + /// For user insteraction + size_t n; + std::cout << "Enter the length of your array (0 to exit): "; + std::cin >> n; + if (n == 0) { + return 0; + } + + int *arr = new int[n]; + std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; + + for (int i = 0; i < n; i++) { + std::cin >> arr[i]; + } + + sorting::insertionSort(arr, n); + + std::cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) { + std::cout << arr[i] << " "; + } + + std::cout << std::endl; + delete[] arr; return 0; } From 330cd9d7f43fd29cbba29f24c4c646f547b2adc7 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:08:38 +0530 Subject: [PATCH 05/12] Created insertion_sort_recursive.cpp file --- sorting/insertion_sort_recursive.cpp | 150 +++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 sorting/insertion_sort_recursive.cpp diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp new file mode 100644 index 00000000000..fd7657c78e8 --- /dev/null +++ b/sorting/insertion_sort_recursive.cpp @@ -0,0 +1,150 @@ +/** + * \file + * \brief [Recursive Insertion Sort Algorithm + * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) + * + * \details + * Insertion sort is a simple sorting algorithm that builds the final + * sorted array one at a time. The recursive version applies the same + * logic but sorts the sub-array recursively before inserting the current + * element in its correct position. + */ + +#include // for std::is_sorted and std::swap +#include // for assert +#include // for std::cout and std::endl +#include // for std::vector + +/** \namespace sorting + * \brief Sorting algorithms + */ +namespace sorting { +/** + * \brief Recursive Insertion Sort Function for an array + * + * @tparam T Type of the elements in the array + * @param[in,out] arr Pointer to the array to be sorted + * @param[in] n Size of the array + */ +template +void recursiveInsertionSort(T *arr, int n) { + // Base case: Array of size 1 is already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted array + T last = arr[n - 1]; + int j = n - 2; + + // Move elements of arr[0..n-1] that are greater than last to one + // position ahead of their current position + while (j >= 0 && arr[j] > last) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = last; +} + +/** + * \brief Recursive Insertion Sort Function for a vector + * + * @tparam T Type of the elements in the vector + * @param[in,out] arr Pointer to the vector to be sorted + * @param[in] n Size of the vector + */ +template +void recursiveInsertionSort(std::vector *arr, size_t n) { + // Base case: If the vector has one or fewer elements, it's already sorted + if (n <= 1) { + return; + } + + // Sort the first n-1 elements recursively + recursiveInsertionSort(arr, n - 1); + + // Insert the nth element at its correct position in the sorted vector + T last = arr->at(n - 1); + int j = n - 2; + + while (j >= 0 && arr->at(j) > last) { + arr->at(j + 1) = arr->at(j); + j--; + } + arr->at(j + 1) = last; +} + +} // namespace sorting + +/** + * \brief Helper function to create a random array + * + * @tparam T Type of array elements + * @param[out] arr Pointer to the array to be filled + * @param[in] N Size of the array + */ +template +static void create_random_array(T *arr, int N) { + while (N--) { + double r = (std::rand() % 10000 - 5000) / 100.f; + arr[N] = static_cast(r); + } +} + +/** + * \brief Test cases to validate the recursive insertion sort algorithm + */ +void tests() { + int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; + std::cout << "Test 1... "; + sorting::recursiveInsertionSort(arr1, 10); + assert(std::is_sorted(arr1, arr1 + 10)); + std::cout << "passed" << std::endl; + + int arr2[5] = {5, -3, 7, -2, 1}; + std::cout << "Test 2... "; + sorting::recursiveInsertionSort(arr2, 5); + assert(std::is_sorted(arr2, arr2 + 5)); + std::cout << "passed" << std::endl; + + float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; + std::cout << "Test 3... "; + sorting::recursiveInsertionSort(arr3, 5); + assert(std::is_sorted(arr3, arr3 + 5)); + std::cout << "passed" << std::endl; + + std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); + std::cout << "Test 4... "; + sorting::recursiveInsertionSort(&arr4, arr4.size()); + assert(std::is_sorted(std::begin(arr4), std::end(arr4))); + std::cout << "passed" << std::endl; + + int arr5[50]; + std::cout << "Test 5... "; + create_random_array(arr5, 50); + sorting::recursiveInsertionSort(arr5, 50); + assert(std::is_sorted(arr5, arr5 + 50)); + std::cout << "passed" << std::endl; + + float arr6[50]; + std::cout << "Test 6... "; + create_random_array(arr6, 50); + sorting::recursiveInsertionSort(arr6, 50); + assert(std::is_sorted(arr6, arr6 + 50)); + std::cout << "passed" << std::endl; +} + +/** + * \brief Main function + * + * Empty except for the call to `tests()`. + * + * @return 0 Always returns 0. + */ +int main() { + tests(); + return 0; +} From ab76cac5741626ff01a45aad1fe3c131f7131e26 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sat, 5 Oct 2024 23:42:10 +0530 Subject: [PATCH 06/12] Updated documentation and structure in insertion_sort_recursive.cpp --- sorting/insertion_sort_recursive.cpp | 151 ++++++++++++++------------- 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index fd7657c78e8..ed2ef369faf 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -1,90 +1,89 @@ /** - * \file - * \brief [Recursive Insertion Sort Algorithm - * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) + * @file + * @brief Insertion Sort Algorithm * - * \details + * @details * Insertion sort is a simple sorting algorithm that builds the final - * sorted array one at a time. The recursive version applies the same - * logic but sorts the sub-array recursively before inserting the current - * element in its correct position. + * sorted array one element at a time. It is much less efficient compared + * to other sorting algorithms like heap sort, merge sort, or quick sort. + * + * However, it has several advantages: + * - Easy to implement. + * - Efficient for small data sets. + * - More efficient than other O(n²) algorithms like selection sort or bubble sort. + * - Stable: it does not change the relative order of elements with equal keys. + * + * Insertion sort works similarly to how people sort playing cards in their hands. + * The algorithm iterates through the list and inserts each element into its correct + * position in the sorted portion of the array. + * + * Example execution: + * 1. Start with the array [4, 3, 2, 5, 1]. + * 2. Insert 3 in its correct position: [3, 4, 2, 5, 1]. + * 3. Insert 2: [2, 3, 4, 5, 1]. + * 4. Continue this until the array is sorted: [1, 2, 3, 4, 5]. */ -#include // for std::is_sorted and std::swap -#include // for assert -#include // for std::cout and std::endl -#include // for std::vector +#include ///< for std::is_sorted +#include ///< for assert function in testing +#include ///< for std::cout and std::endl +#include ///< for using std::vector -/** \namespace sorting - * \brief Sorting algorithms +/** + * @namespace sorting + * @brief Contains sorting algorithms */ namespace sorting { -/** - * \brief Recursive Insertion Sort Function for an array - * - * @tparam T Type of the elements in the array - * @param[in,out] arr Pointer to the array to be sorted - * @param[in] n Size of the array + +/** + * @brief Insertion Sort Function + * + * @tparam T Type of the array elements + * @param [in,out] arr Array to be sorted + * @param n Size of the array */ template -void recursiveInsertionSort(T *arr, int n) { - // Base case: Array of size 1 is already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted array - T last = arr[n - 1]; - int j = n - 2; - - // Move elements of arr[0..n-1] that are greater than last to one - // position ahead of their current position - while (j >= 0 && arr[j] > last) { - arr[j + 1] = arr[j]; - j--; +void insertionSort(T *arr, int n) { + for (int i = 1; i < n; i++) { + T temp = arr[i]; + int j = i - 1; + while (j >= 0 && temp < arr[j]) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = temp; } - arr[j + 1] = last; } -/** - * \brief Recursive Insertion Sort Function for a vector - * - * @tparam T Type of the elements in the vector - * @param[in,out] arr Pointer to the vector to be sorted - * @param[in] n Size of the vector +/** + * @brief Insertion Sort for a vector + * + * @tparam T Type of the vector elements + * @param [in,out] arr Pointer to the vector to be sorted */ template -void recursiveInsertionSort(std::vector *arr, size_t n) { - // Base case: If the vector has one or fewer elements, it's already sorted - if (n <= 1) { - return; - } - - // Sort the first n-1 elements recursively - recursiveInsertionSort(arr, n - 1); - - // Insert the nth element at its correct position in the sorted vector - T last = arr->at(n - 1); - int j = n - 2; - - while (j >= 0 && arr->at(j) > last) { - arr->at(j + 1) = arr->at(j); - j--; +void insertionSort(std::vector *arr) { + size_t n = arr->size(); + + for (size_t i = 1; i < n; i++) { + T temp = arr->at(i); + int32_t j = i - 1; + while (j >= 0 && temp < arr->at(j)) { + arr->at(j + 1) = arr->at(j); + j--; + } + arr->at(j + 1) = temp; } - arr->at(j + 1) = last; } } // namespace sorting -/** - * \brief Helper function to create a random array - * - * @tparam T Type of array elements - * @param[out] arr Pointer to the array to be filled - * @param[in] N Size of the array +/** + * @brief Helper function to create a random array + * + * @tparam T Type of the array elements + * @param arr Array to fill (must be pre-allocated) + * @param N Number of elements in the array */ template static void create_random_array(T *arr, int N) { @@ -95,56 +94,58 @@ static void create_random_array(T *arr, int N) { } /** - * \brief Test cases to validate the recursive insertion sort algorithm + * @brief Test Cases for the sorting algorithm */ void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; std::cout << "Test 1... "; - sorting::recursiveInsertionSort(arr1, 10); + sorting::insertionSort(arr1, 10); assert(std::is_sorted(arr1, arr1 + 10)); std::cout << "passed" << std::endl; int arr2[5] = {5, -3, 7, -2, 1}; std::cout << "Test 2... "; - sorting::recursiveInsertionSort(arr2, 5); + sorting::insertionSort(arr2, 5); assert(std::is_sorted(arr2, arr2 + 5)); std::cout << "passed" << std::endl; float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8}; std::cout << "Test 3... "; - sorting::recursiveInsertionSort(arr3, 5); + sorting::insertionSort(arr3, 5); assert(std::is_sorted(arr3, arr3 + 5)); std::cout << "passed" << std::endl; std::vector arr4({5.6, -3.1, -3.0, -2.1, 1.8}); std::cout << "Test 4... "; - sorting::recursiveInsertionSort(&arr4, arr4.size()); + sorting::insertionSort(&arr4); assert(std::is_sorted(std::begin(arr4), std::end(arr4))); std::cout << "passed" << std::endl; int arr5[50]; std::cout << "Test 5... "; create_random_array(arr5, 50); - sorting::recursiveInsertionSort(arr5, 50); + sorting::insertionSort(arr5, 50); assert(std::is_sorted(arr5, arr5 + 50)); std::cout << "passed" << std::endl; float arr6[50]; std::cout << "Test 6... "; create_random_array(arr6, 50); - sorting::recursiveInsertionSort(arr6, 50); + sorting::insertionSort(arr6, 50); assert(std::is_sorted(arr6, arr6 + 50)); std::cout << "passed" << std::endl; } /** - * \brief Main function + * @brief Main function * * Empty except for the call to `tests()`. * * @return 0 Always returns 0. */ int main() { + /// Running predefined tests to test the algorithm tests(); + return 0; } From c026e8cfa14ccdab982f25ad4bb6782eced8dec8 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sun, 6 Oct 2024 10:54:30 +0530 Subject: [PATCH 07/12] Updated documentation in insertion_sort_recursive.cpp --- sorting/insertion_sort_recursive.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index ed2ef369faf..ae4ed00cbe5 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -1,7 +1,8 @@ /** * @file * @brief Insertion Sort Algorithm - * + * @author [Dhanush S] (https://github.com/Fandroid745) + * * @details * Insertion sort is a simple sorting algorithm that builds the final * sorted array one element at a time. It is much less efficient compared @@ -16,7 +17,10 @@ * Insertion sort works similarly to how people sort playing cards in their hands. * The algorithm iterates through the list and inserts each element into its correct * position in the sorted portion of the array. - * + * + * The time complexity of the algorithm is \f$O(n^2)\f$, and in some cases, it + * can be \f$O(n)\f$. + * * Example execution: * 1. Start with the array [4, 3, 2, 5, 1]. * 2. Insert 3 in its correct position: [3, 4, 2, 5, 1]. @@ -24,10 +28,11 @@ * 4. Continue this until the array is sorted: [1, 2, 3, 4, 5]. */ -#include ///< for std::is_sorted -#include ///< for assert function in testing -#include ///< for std::cout and std::endl -#include ///< for using std::vector + +#include /// for std::is_sorted +#include /// for assert function in testing +#include /// for std::cout and std::endl +#include /// for using std::vector /** * @namespace sorting @@ -39,7 +44,7 @@ namespace sorting { * @brief Insertion Sort Function * * @tparam T Type of the array elements - * @param [in,out] arr Array to be sorted + * @param[in,out] arr Array to be sorted * @param n Size of the array */ template @@ -96,7 +101,7 @@ static void create_random_array(T *arr, int N) { /** * @brief Test Cases for the sorting algorithm */ -void tests() { +static void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; std::cout << "Test 1... "; sorting::insertionSort(arr1, 10); @@ -141,11 +146,11 @@ void tests() { * * Empty except for the call to `tests()`. * - * @return 0 Always returns 0. + * @return 0 on successful exit. */ int main() { /// Running predefined tests to test the algorithm - tests(); + tests(); /// run self test implementations return 0; } From 16250de75a21adba686be4bee42bbb086e4dc505 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:23:50 +0530 Subject: [PATCH 08/12] chore: remove redundant comment --- sorting/insertion_sort_recursive.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index ae4ed00cbe5..c1cee3b9b33 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -149,7 +149,6 @@ static void tests() { * @return 0 on successful exit. */ int main() { - /// Running predefined tests to test the algorithm tests(); /// run self test implementations return 0; From d7379e4b46caf7ec46e56640a602d71e3ff0888e Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Sun, 6 Oct 2024 14:21:45 +0530 Subject: [PATCH 09/12] chore:Removed unnecssary whitespace --- sorting/insertion_sort_recursive.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index c1cee3b9b33..5aec9e7432a 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -1,7 +1,7 @@ /** * @file * @brief Insertion Sort Algorithm - * @author [Dhanush S] (https://github.com/Fandroid745) + * @author [Dhanush S](https://github.com/Fandroid745) * * @details * Insertion sort is a simple sorting algorithm that builds the final From 95b38a1f5e766d0bc2c54646e7b93645d5324b96 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Mon, 7 Oct 2024 05:57:23 +0530 Subject: [PATCH 10/12] doc: ease the brief of test --- sorting/insertion_sort_recursive.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index 5aec9e7432a..214d485ddfc 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -99,7 +99,8 @@ static void create_random_array(T *arr, int N) { } /** - * @brief Test Cases for the sorting algorithm + * @brief self test implementation + * @return void */ static void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; From 3664e4ef092e1c413ff7fd0261de31e766746971 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Mon, 7 Oct 2024 05:57:45 +0530 Subject: [PATCH 11/12] doc: remove redundant lines from main --- sorting/insertion_sort_recursive.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index 214d485ddfc..474cedffe9b 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -144,9 +144,6 @@ static void tests() { /** * @brief Main function - * - * Empty except for the call to `tests()`. - * * @return 0 on successful exit. */ int main() { From c35394ed4653447ea1bfe81234491b8481751ec2 Mon Sep 17 00:00:00 2001 From: Dhanush S <95139774+Fandroid745@users.noreply.github.com> Date: Mon, 7 Oct 2024 06:35:11 +0530 Subject: [PATCH 12/12] chore:Removed redundant whitespace --- sorting/insertion_sort_recursive.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sorting/insertion_sort_recursive.cpp b/sorting/insertion_sort_recursive.cpp index 474cedffe9b..256a27e1ce3 100644 --- a/sorting/insertion_sort_recursive.cpp +++ b/sorting/insertion_sort_recursive.cpp @@ -148,6 +148,5 @@ static void tests() { */ int main() { tests(); /// run self test implementations - return 0; }