From 957b57cf60267d0018a3901bbf9ae20b6054c57f Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 10:10:51 -0400 Subject: [PATCH 1/7] fix issues from #929 --- sorting/insertion_sort.cpp | 106 +++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 39 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 39298eb45..4bfce9ce6 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -4,22 +4,18 @@ * \brief [Insertion Sort Algorithm * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) * - * \author - * * \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.
- * 
+ * 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. @@ -27,44 +23,41 @@ * of elements as soon as we find a unsorted element that is a misplaced * element we place it at a sorted position. * - * Suppose initially we have - *
- * 4 3 2 5 1
- *
- * we start traversing from 4 till we reach 1
+ * Execution example steps:
+ * 1. Suppose initially we have
+ * 
4 3 2 5 1
+ * 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 - * - * 3 4 2 5 1 - * - * in the next iteration we are at 2 we find that this is also misplaced so + *
3 4 2 5 1
+ * 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 - * - * 2 3 4 5 1 - * - * we does 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 - * - * 1 2 3 4 5 - *
- * + *
2 3 4 5 1
+ * 4. we does 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 + *
1 2 3 4 5
*/ #include #include #include +#include +/** \namespace sorting + * \brief Sorting algorithms + */ +namespace sorting { /** \brief * Insertion Sort Function * * @param arr Array to be sorted * @param n Size of Array - * */ -void insertionSort(int *arr, int n) { +template +void insertionSort(T *arr, int n) { for (int i = 1; i < n; i++) { - int temp = arr[i]; + T temp = arr[i]; int j = i - 1; while (j >= 0 && temp < arr[j]) { arr[j + 1] = arr[j]; @@ -74,17 +67,48 @@ void insertionSort(int *arr, int n) { } } +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; + } +} + +} // namespace sorting + /** Test Cases to test algorithm */ void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; - insertionSort(arr1, 10); + std::cout << "Test 1... "; + sorting::insertionSort(arr1, 10); assert(std::is_sorted(arr1, arr1 + 10)); - std::cout << "Test 1 Passed" << std::endl; + std::cout << "passed" << std::endl; int arr2[5] = {5, -3, 7, -2, 1}; - insertionSort(arr2, 5); + std::cout << "Test 2... "; + sorting::insertionSort(arr2, 5); assert(std::is_sorted(arr2, arr2 + 5)); - std::cout << "Test 2 Passed" << std::endl; + 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); + 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); + assert(std::is_sorted(std::begin(arr4), std::end(arr4))); + std::cout << "passed" << std::endl; } /** Main Function */ @@ -93,9 +117,13 @@ int main() { tests(); /// For user insteraction - int n; - std::cout << "Enter the length of your array : "; + 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 : "; @@ -103,7 +131,7 @@ int main() { std::cin >> arr[i]; } - insertionSort(arr, n); + sorting::insertionSort(arr, n); std::cout << "\nSorted Array : "; for (int i = 0; i < n; i++) { From 9b799c93cb9977c66363958b9f5157841d51a768 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 10:19:18 -0400 Subject: [PATCH 2/7] added documentation for new function --- sorting/insertion_sort.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 4bfce9ce6..32315a87a 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -51,7 +51,7 @@ namespace sorting { /** \brief * Insertion Sort Function * - * @param arr Array to be sorted + * @param [in,out] arr Array to be sorted * @param n Size of Array */ template @@ -67,6 +67,10 @@ void insertionSort(T *arr, int n) { } } +/** Insertion Sort Function + * + * @param [in,out] arr pointer to array to be sorted + */ template void insertionSort(std::vector *arr) { size_t n = arr->size(); From 69b5c8395a1d392ba6d1011e8aed1be74cfcf54b Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 10:36:08 -0400 Subject: [PATCH 3/7] better rendering of example steps --- sorting/insertion_sort.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 32315a87a..b6a8f72ac 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -23,20 +23,20 @@ * of elements as soon as we find a unsorted element that is a misplaced * element we place it at a sorted position. * - * Execution example steps: + * Example execution steps: * 1. Suppose initially we have - *
4 3 2 5 1
+ * \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 - *
3 4 2 5 1
+ * \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 - *
2 3 4 5 1
+ * \f{bmatrix}{2 &3 &4 &5 &1\f} * 4. we does 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 - *
1 2 3 4 5
+ * \f{bmatrix}{1 &2 &3 &4 &5\f} */ #include From fed7dd1c13d75a736b90260a1d39355732b4e8e5 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 12:37:54 -0400 Subject: [PATCH 4/7] improved self-tests --- sorting/insertion_sort.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index b6a8f72ac..34a55d21a 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -51,6 +51,7 @@ namespace sorting { /** \brief * Insertion Sort Function * + * @tparam T type of array * @param [in,out] arr Array to be sorted * @param n Size of Array */ @@ -69,6 +70,7 @@ void insertionSort(T *arr, int n) { /** Insertion Sort Function * + * @tparam T type of array * @param [in,out] arr pointer to array to be sorted */ template @@ -88,6 +90,21 @@ void insertionSort(std::vector *arr) { } // namespace sorting +/** + * @brief Create a random array objecthelper 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 + */ +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); + } +} + /** Test Cases to test algorithm */ void tests() { int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; @@ -113,6 +130,20 @@ void tests() { 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::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::insertionSort(arr6, 50); + assert(std::is_sorted(arr6, arr6 + 50)); + std::cout << "passed" << std::endl; } /** Main Function */ From 84fd1cb0d01f2d7a33f07adecf8fc4b6088a4fe6 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 13:04:54 -0400 Subject: [PATCH 5/7] small case 'e' Co-authored-by: Ayaan Khan --- sorting/insertion_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 34a55d21a..efa60e584 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -9,7 +9,7 @@ * 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 + * 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. From 4045bcddf3752d5b2d8efac6ce132c79ff53c7e7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 13:05:52 -0400 Subject: [PATCH 6/7] verb correction --- sorting/insertion_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index efa60e584..beb828005 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -34,7 +34,7 @@ * 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 does not do anything with 5 and move on to the next iteration and + * 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} */ From 390ee8428e7f69a609f682c7ddede7279ae187e7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 1 Jul 2020 13:08:41 -0400 Subject: [PATCH 7/7] fix to upper case --- sorting/insertion_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index beb828005..c9bac4bf7 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -34,7 +34,7 @@ * 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 + * 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} */