From b964f9af009a35018059a4bc08653774b71ee61a Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Fri, 26 Jun 2020 17:04:19 +0530 Subject: [PATCH 1/8] fix: minor typo --- numerical_methods/ordinary_least_squares_regressor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numerical_methods/ordinary_least_squares_regressor.cpp b/numerical_methods/ordinary_least_squares_regressor.cpp index bbd75a742..832d6edf7 100644 --- a/numerical_methods/ordinary_least_squares_regressor.cpp +++ b/numerical_methods/ordinary_least_squares_regressor.cpp @@ -369,7 +369,7 @@ int main() { std::vector Y(N); std::cout - << "Enter training data. Per sample, provide features ad one output." + << "Enter training data. Per sample, provide features and one output." << std::endl; for (size_t rows = 0; rows < N; rows++) { From 1dfbdde3c62dfcd67d4769dcc8c16904527f8b22 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 1 Jul 2020 03:28:12 +0530 Subject: [PATCH 2/8] resolve merge conflicts --- numerical_methods/ordinary_least_squares_regressor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numerical_methods/ordinary_least_squares_regressor.cpp b/numerical_methods/ordinary_least_squares_regressor.cpp index 832d6edf7..bbd75a742 100644 --- a/numerical_methods/ordinary_least_squares_regressor.cpp +++ b/numerical_methods/ordinary_least_squares_regressor.cpp @@ -369,7 +369,7 @@ int main() { std::vector Y(N); std::cout - << "Enter training data. Per sample, provide features and one output." + << "Enter training data. Per sample, provide features ad one output." << std::endl; for (size_t rows = 0; rows < N; rows++) { From d3324aaf34b4fde750192ee9259b62835676a0cf Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 1 Jul 2020 03:54:01 +0530 Subject: [PATCH 3/8] Fix: code quality and Docs --- sorting/insertion_sort.cpp | 103 ++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 23 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index fe920ca59..180330a60 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -1,36 +1,93 @@ -// Insertion Sort +/** + * + * \file + * \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.
+ * 
+ * + * 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. + * + * Suppose initially we have + *
+ * 4 3 2 5 1
+ * 
+ * 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
+ * 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
+ * 
+ * + */ #include -int main() { - int n; - std::cout << "\nEnter the length of your array : "; - std::cin >> n; - int *Array = new int[n]; - std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : "; - - // Input - for (int i = 0; i < n; i++) { - std::cin >> Array[i]; - } - - // Sorting +/** \brief + * Insertion Sort Function + * + * @param arr Array to be sorted + * @param n Size of Array + * + */ +void insertionSort(int *arr, int n) { for (int i = 1; i < n; i++) { - int temp = Array[i]; + int temp = arr[i]; int j = i - 1; - while (j >= 0 && temp < Array[j]) { - Array[j + 1] = Array[j]; + while (j >= 0 && temp < arr[j]) { + arr[j + 1] = arr[j]; j--; } - Array[j + 1] = temp; + arr[j + 1] = temp; } +} + +/** Main Function */ +int main() { + int n; + std::cout << "Enter the length of your array : "; + std::cin >> n; + 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]; + + insertionSort(arr, n); - // Output std::cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) { - std::cout << Array[i] << "\t"; - } + for (int i = 0; i < n; i++) std::cout << arr[i] << " "; - delete[] Array; + std::cout << std::endl; + delete[] arr; return 0; } From d64f17f94c9278fd072e2e2a3477646bd9d5e5bf Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:26:08 +0000 Subject: [PATCH 4/8] formatting source-code for 9217b4d8a880915d0a53b2408f38325f4b864e79 --- sorting/insertion_sort.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 180330a60..12c1ead69 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -26,15 +26,15 @@ * 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. - * + * * Suppose initially we have *
  * 4 3 2 5 1
- * 
+ *
  * 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
@@ -55,7 +55,7 @@
 
 /** \brief
  * Insertion Sort Function
- * 
+ *
  * @param arr Array to be sorted
  * @param n Size of Array
  *
@@ -80,12 +80,12 @@ int main() {
     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];
+    for (int i = 0; i < n; i++) std::cin >> arr[i];
 
     insertionSort(arr, n);
 
     std::cout << "\nSorted Array : ";
-    for (int i = 0; i < n; i++)  std::cout << arr[i] << " ";
+    for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
 
     std::cout << std::endl;
     delete[] arr;

From 8179fbfdec72dda8ea795ad1198525e41dd12740 Mon Sep 17 00:00:00 2001
From: Ayaan Khan 
Date: Wed, 1 Jul 2020 04:03:13 +0530
Subject: [PATCH 5/8] test cases added

---
 sorting/insertion_sort.cpp | 43 +++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp
index 180330a60..cc3216c1e 100644
--- a/sorting/insertion_sort.cpp
+++ b/sorting/insertion_sort.cpp
@@ -52,6 +52,8 @@
  */
 
 #include 
+#include 
+#include 
 
 /** \brief
  * Insertion Sort Function
@@ -72,22 +74,39 @@ void insertionSort(int *arr, int n) {
     }
 }
 
+/** Test Cases to test algorithm */
+void tests() {
+  int arr1[10] = { 78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
+  insertionSort(arr1, 10);
+  assert(std::is_sorted(arr1, arr1 + 10));
+  std::cout << "Test 1 Passed" << std::endl;
+
+  int arr2[5] = {5, -3, 7, -2, 1};
+  insertionSort(arr2, 5);
+  assert(std::is_sorted(arr2, arr2 + 5));
+  std::cout << "Test 2 Passed" << std::endl;
+}
+
 /** Main Function */
 int main() {
-    int n;
-    std::cout << "Enter the length of your array : ";
-    std::cin >> n;
-    int *arr = new int[n];
-    std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
+  /// Running predefined tests to test algorithm
+  tests();
 
-    for (int i = 0; i < n; i++)  std::cin >> arr[i];
+  /// For user insteraction
+  int n;
+  std::cout << "Enter the length of your array : ";
+  std::cin >> n;
+  int *arr = new int[n];
+  std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
 
-    insertionSort(arr, n);
+  for (int i = 0; i < n; i++)  std::cin >> arr[i];
 
-    std::cout << "\nSorted Array : ";
-    for (int i = 0; i < n; i++)  std::cout << arr[i] << " ";
+  insertionSort(arr, n);
 
-    std::cout << std::endl;
-    delete[] arr;
-    return 0;
+  std::cout << "\nSorted Array : ";
+  for (int i = 0; i < n; i++)  std::cout << arr[i] << " ";
+
+  std::cout << std::endl;
+  delete[] arr;
+  return 0;
 }

From 3e1edca0242d83280bd9088a8ea20839cfa7c8c6 Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Tue, 30 Jun 2020 22:35:45 +0000
Subject: [PATCH 6/8] formatting source-code for
 efcccd0148923a03eea0fe95c3e27c38e2d9c46d

---
 sorting/insertion_sort.cpp | 48 +++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp
index 5b20eef47..877e98f27 100644
--- a/sorting/insertion_sort.cpp
+++ b/sorting/insertion_sort.cpp
@@ -51,9 +51,9 @@
  *
  */
 
-#include 
 #include 
 #include 
+#include 
 
 /** \brief
  * Insertion Sort Function
@@ -76,37 +76,37 @@ void insertionSort(int *arr, int n) {
 
 /** Test Cases to test algorithm */
 void tests() {
-  int arr1[10] = { 78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
-  insertionSort(arr1, 10);
-  assert(std::is_sorted(arr1, arr1 + 10));
-  std::cout << "Test 1 Passed" << std::endl;
+    int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
+    insertionSort(arr1, 10);
+    assert(std::is_sorted(arr1, arr1 + 10));
+    std::cout << "Test 1 Passed" << std::endl;
 
-  int arr2[5] = {5, -3, 7, -2, 1};
-  insertionSort(arr2, 5);
-  assert(std::is_sorted(arr2, arr2 + 5));
-  std::cout << "Test 2 Passed" << std::endl;
+    int arr2[5] = {5, -3, 7, -2, 1};
+    insertionSort(arr2, 5);
+    assert(std::is_sorted(arr2, arr2 + 5));
+    std::cout << "Test 2 Passed" << std::endl;
 }
 
 /** Main Function */
 int main() {
-  /// Running predefined tests to test algorithm
-  tests();
+    /// Running predefined tests to test algorithm
+    tests();
 
-  /// For user insteraction
-  int n;
-  std::cout << "Enter the length of your array : ";
-  std::cin >> n;
-  int *arr = new int[n];
-  std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
+    /// For user insteraction
+    int n;
+    std::cout << "Enter the length of your array : ";
+    std::cin >> n;
+    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];
+    for (int i = 0; i < n; i++) std::cin >> arr[i];
 
-  insertionSort(arr, n);
+    insertionSort(arr, n);
 
-  std::cout << "\nSorted Array : ";
-  for (int i = 0; i < n; i++)  std::cout << arr[i] << " ";
+    std::cout << "\nSorted Array : ";
+    for (int i = 0; i < n; i++) std::cout << arr[i] << " ";
 
-  std::cout << std::endl;
-  delete[] arr;
-  return 0;
+    std::cout << std::endl;
+    delete[] arr;
+    return 0;
 }

From dd0a07f18ae5d2e5d78bb657861a131408a7c642 Mon Sep 17 00:00:00 2001
From: Ayaan Khan 
Date: Wed, 1 Jul 2020 15:38:55 +0530
Subject: [PATCH 7/8] loop formating fix

---
 sorting/insertion_sort.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp
index 5b20eef47..07cb7375f 100644
--- a/sorting/insertion_sort.cpp
+++ b/sorting/insertion_sort.cpp
@@ -99,12 +99,16 @@ int main() {
   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];
+  for (int i = 0; i < n; i++) {
+    std::cin >> arr[i];
+  }
 
   insertionSort(arr, n);
 
   std::cout << "\nSorted Array : ";
-  for (int i = 0; i < n; i++)  std::cout << arr[i] << " ";
+  for (int i = 0; i < n; i++) {
+    std::cout << arr[i] << " ";
+  }
 
   std::cout << std::endl;
   delete[] arr;

From e4c62d28fec2822c7f4857414e5bf76bdc5cf611 Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Wed, 1 Jul 2020 10:10:51 +0000
Subject: [PATCH 8/8] formatting source-code for
 7af31e32a22952a70682a926866c1ff1bab72b99

---
 sorting/insertion_sort.cpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp
index ceac13903..39298eb45 100644
--- a/sorting/insertion_sort.cpp
+++ b/sorting/insertion_sort.cpp
@@ -99,16 +99,16 @@ int main() {
     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];
-  }
+    for (int i = 0; i < n; i++) {
+        std::cin >> arr[i];
+    }
 
     insertionSort(arr, n);
 
-  std::cout << "\nSorted Array : ";
-  for (int i = 0; i < n; i++) {
-    std::cout << arr[i] << " ";
-  }
+    std::cout << "\nSorted Array : ";
+    for (int i = 0; i < n; i++) {
+        std::cout << arr[i] << " ";
+    }
 
     std::cout << std::endl;
     delete[] arr;