From 46e07288ba5b259ae2fa7b32c289b0305ef2def0 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 21:00:52 +0530 Subject: [PATCH 01/31] Fixed Bug [munmap_chunck() core dumped] --- sorting/merge_sort.cpp | 49 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index fc6d66af5..d6f099cea 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -1,36 +1,37 @@ /** * \addtogroup sorting Sorting Algorithms - * @{ + * @file{ * \file - * \brief [Merege Sort Algorithm + * \breif [Merege Sort Algorithm * (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation * - * \author [Ayaan Khan](http://github.com/ayaankhan98) + * \author [Ayaan Khan] (http://github.com/ayaankhan98) * - * \details * Merge Sort is an efficient, general purpose, comparison * based sorting algorithm. * Merge Sort is a divide and conquer algorithm - * + * */ + #include /** * - * The merge() function is used for merging two halves. - * The merge(arr, l, m, r) is key process that assumes that - * arr[l..m] and arr[m+1..r] are sorted and merges the two + * The merge() function is used for merging two halves. + * The merge(arr, l, m, r) is key process that assumes that + * arr[l..m] and arr[m+1..r] are sorted and merges the two * sorted sub-arrays into one. * - * @param arr - array with two halves arr[l...m] and arr[m+1...l] - * @param l - left index or start index of first half array - * @param m - right index or end index of first half array - * + * @param arr[] is the array with two halves one is arr[l...m] and + * other is arr[m+1...l] + * @param l is the left index of first half array + * @param m is the end index of right index of first half array + * * (The second array starts form m+1 and goes till l) - * - * @param l - end index or right index of second half array + * + * @param l is the end index of right index of second half array */ -void merge(int *arr, int l, int m, int r) { +void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; @@ -75,12 +76,12 @@ void merge(int *arr, int l, int m, int r) { * input array into two halves and calls itself for the two halves * and then calls merge() to merge the two halves * - * @param arr - array to be sorted - * @param l - left index or start index of array - * @param r - right index or end index of array + * @param arr[] the array which is to be sorted + * @param l define the left index of array + * @param r defines the right index of array * */ -void mergeSort(int *arr, int l, int r) { +void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); @@ -90,11 +91,11 @@ void mergeSort(int *arr, int l, int r) { } /** - * Utility function used to print the array after + * A simple utility function used to print the array after * sorting */ -void show(int *arr, int size) { - for (int i = 0; i < size; i++) std::cout << arr[i] << " "; +void show(int arr[], int size) { + for (int i = 0; i < size; i++) std::cout << arr[i] << " "; std::cout << "\n"; } @@ -108,9 +109,9 @@ int main() { for (int i = 0; i < size; ++i) { std::cin >> arr[i]; } - mergeSort(arr, 0, size - 1); + mergeSort(arr, 0, size-1); std::cout << "Sorted array : "; - show(arr, size - 1); + show(arr, size-1); delete[] arr; return 0; } From 5e63bede6fc40e1e5a07abea8d36748200332b27 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 21:02:50 +0530 Subject: [PATCH 02/31] fixed line spacing --- sorting/merge_sort.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index d6f099cea..87d1cd78d 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -12,7 +12,6 @@ * Merge Sort is a divide and conquer algorithm * */ - #include /** From b1d2ad1dfc033a0070479a6f680be3bf876dd03e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 20 Jun 2020 15:31:59 +0000 Subject: [PATCH 03/31] formatting source-code for b06bbf4dc6c46a3284d7852bb570438384eef4ef --- sorting/merge_sort.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 87d1cd78d..716c7c64d 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -10,24 +10,24 @@ * Merge Sort is an efficient, general purpose, comparison * based sorting algorithm. * Merge Sort is a divide and conquer algorithm - * + * */ #include /** * - * The merge() function is used for merging two halves. - * The merge(arr, l, m, r) is key process that assumes that - * arr[l..m] and arr[m+1..r] are sorted and merges the two + * The merge() function is used for merging two halves. + * The merge(arr, l, m, r) is key process that assumes that + * arr[l..m] and arr[m+1..r] are sorted and merges the two * sorted sub-arrays into one. * * @param arr[] is the array with two halves one is arr[l...m] and * other is arr[m+1...l] * @param l is the left index of first half array * @param m is the end index of right index of first half array - * + * * (The second array starts form m+1 and goes till l) - * + * * @param l is the end index of right index of second half array */ void merge(int arr[], int l, int m, int r) { @@ -94,7 +94,7 @@ void mergeSort(int arr[], int l, int r) { * sorting */ void show(int arr[], int size) { - for (int i = 0; i < size; i++) std::cout << arr[i] << " "; + for (int i = 0; i < size; i++) std::cout << arr[i] << " "; std::cout << "\n"; } @@ -108,9 +108,9 @@ int main() { for (int i = 0; i < size; ++i) { std::cin >> arr[i]; } - mergeSort(arr, 0, size-1); + mergeSort(arr, 0, size - 1); std::cout << "Sorted array : "; - show(arr, size-1); + show(arr, size - 1); delete[] arr; return 0; } From 0a09c1e382acef49ebbeaf1ad810e0455ea08206 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 21:05:19 +0530 Subject: [PATCH 04/31] fixed line spacing --- sorting/merge_sort.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 716c7c64d..34600d06a 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -7,6 +7,7 @@ * * \author [Ayaan Khan] (http://github.com/ayaankhan98) * + * \details * Merge Sort is an efficient, general purpose, comparison * based sorting algorithm. * Merge Sort is a divide and conquer algorithm From 99ca279ff6555fd9ef25b1c87d710a3b3bb4300b Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 21:41:25 +0530 Subject: [PATCH 05/31] fixed documentation --- sorting/merge_sort.cpp | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 34600d06a..fc6d66af5 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -1,11 +1,11 @@ /** * \addtogroup sorting Sorting Algorithms - * @file{ + * @{ * \file - * \breif [Merege Sort Algorithm + * \brief [Merege Sort Algorithm * (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation * - * \author [Ayaan Khan] (http://github.com/ayaankhan98) + * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details * Merge Sort is an efficient, general purpose, comparison @@ -22,16 +22,15 @@ * arr[l..m] and arr[m+1..r] are sorted and merges the two * sorted sub-arrays into one. * - * @param arr[] is the array with two halves one is arr[l...m] and - * other is arr[m+1...l] - * @param l is the left index of first half array - * @param m is the end index of right index of first half array + * @param arr - array with two halves arr[l...m] and arr[m+1...l] + * @param l - left index or start index of first half array + * @param m - right index or end index of first half array * * (The second array starts form m+1 and goes till l) * - * @param l is the end index of right index of second half array + * @param l - end index or right index of second half array */ -void merge(int arr[], int l, int m, int r) { +void merge(int *arr, int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; @@ -76,12 +75,12 @@ void merge(int arr[], int l, int m, int r) { * input array into two halves and calls itself for the two halves * and then calls merge() to merge the two halves * - * @param arr[] the array which is to be sorted - * @param l define the left index of array - * @param r defines the right index of array + * @param arr - array to be sorted + * @param l - left index or start index of array + * @param r - right index or end index of array * */ -void mergeSort(int arr[], int l, int r) { +void mergeSort(int *arr, int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); @@ -91,10 +90,10 @@ void mergeSort(int arr[], int l, int r) { } /** - * A simple utility function used to print the array after + * Utility function used to print the array after * sorting */ -void show(int arr[], int size) { +void show(int *arr, int size) { for (int i = 0; i < size; i++) std::cout << arr[i] << " "; std::cout << "\n"; } From 4afe371ba535a837ac67c9a43fae094d5a6949c8 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 22:50:49 +0530 Subject: [PATCH 06/31] closed the paranthesis of line 3 --- sorting/merge_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index fc6d66af5..ce87c8b5c 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -114,4 +114,4 @@ int main() { delete[] arr; return 0; } -/** @} */ +/** @} */ From 5c77f1ebe0d8dcefaf472b90b7a5f3ffd82137c8 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 00:03:36 +0530 Subject: [PATCH 07/31] Bug Fix heap sort [Fresh Implementation] --- sorting/heap_sort.cpp | 110 ++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 69 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index ef9f87094..405e4641d 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -1,31 +1,31 @@ /** + * \addtogroup sorting Sorting Algorithm + * @{ * \file * \brief [Heap Sort Algorithm - * (heap sort)](https://en.wikipedia.org/wiki/Heapsort) implementation + * (HEAP SORT)](https://en.wikipedia.org/wiki/Heapsort) implementation * * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details - * Heap-sort is a comparison-based sorting algorithm. - * Heap-sort can be thought of as an improved selection sort: - * like selection sort, heap sort divides its input into a sorted - * and an unsorted region, and it iteratively shrinks the unsorted - * region by extracting the largest element from it and inserting - * it into the sorted region. Unlike selection sort, - * heap sort does not waste time with a linear-time scan of the - * unsorted region; rather, heap sort maintains the unsorted region - * in a heap data structure to more quickly find the largest element + * heapsort is a comparison-based sorting algorithm. + * Heapsort can be thought of as an improved selection sort: + * like selection sort, heapsort divides its input into a sorted + * and an unsorted region, and it iteratively shrinks the unsorted + * region by extracting the largest element from it and inserting + * it into the sorted region. Unlike selection sort, + * heapsort does not waste time with a linear-time scan of the + * unsorted region; rather, heap sort maintains the unsorted region + * in a heap data structure to more quickly find the largest element * in each step. - * - * Time Complexity - \f$O(n \log(n))\f$ + * + * Time Complexity - O(nlog(n)) * */ -#include -#include #include /** - * + * * Utility Lambda function to print the array after * sorting. * @@ -33,32 +33,25 @@ * @param sz size of array * */ -template -void printArray(T *arr, int sz) { - for (int i = 0; i < sz; i++) std::cout << arr[i] << " "; +auto printArray = [] (int *arr, int sz) { + for (int i = 0 ; i < sz ; i++) + std::cout << arr[i] <<" "; std::cout << "\n"; -} +}; /** * - * \addtogroup sorting Sorting Algorithm - * @{ - * - * The heapify procedure can be thought of as building a heap from - * the bottom up by successively sifting downward to establish the - * heap property. - * - * @param arr array to be sorted - * @param n size of array - * @param i node position in Binary Tress or element position in - * Array to be compared with it's childern - * + * The heapify procedure can be thought of as building a heap from + * the bottom up by successively sifting downward to establish the + * heap property. + * + * @param arr array be to sorted + * @param */ -template -void heapify(T *arr, int n, int i) { +void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) { int largest = i; - int l = 2 * i + 1; - int r = 2 * i + 2; + int l = 2*i + 1; + int r = 2*i + 2; if (l < n && arr[l] > arr[largest]) largest = l; @@ -70,54 +63,33 @@ void heapify(T *arr, int n, int i) { std::swap(arr[i], arr[largest]); heapify(arr, n, largest); } -} +}; /** - * Utilizes heapify procedure to sort + * heapSort lambda function utilizes heapify procedure to sort * the array * * @param arr array to be sorted * @param n size of array * */ -template -void heapSort(T *arr, int n) { - for (int i = n - 1; i >= 0; i--) heapify(arr, n, i); +auto heapSort = [] (int *arr, int n) { + for (int i = n-1 ; i >= 0; i--) + heapify(arr, n, i); - for (int i = n - 1; i >= 0; i--) { + for (int i = n-1 ; i >= 0; i--) { std::swap(arr[0], arr[i]); heapify(arr, i, 0); } -} - -/** - * - * @} - * Test cases to test the program - * - */ -void test() { - std::cout << "Test 1\n"; - int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; - int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array - printArray(arr, sz); // displaying the array before sorting - heapSort(arr, sz); // calling heapsort to sort the array - printArray(arr, sz); // display array after sorting - assert(std::is_sorted(arr, arr + sz)); - std::cout << "Test 1 Passed\n========================\n"; - - std::cout << "Test 2\n"; - double arr2[] = {4.5, -3.6, 7.6, 0, 12.9}; - sz = sizeof(arr2) / sizeof(arr2[0]); - printArray(arr2, sz); - heapSort(arr2, sz); - printArray(arr2, sz); - assert(std::is_sorted(arr2, arr2 + sz)); - std::cout << "Test 2 passed\n"; -} +}; /** Main function */ int main() { - test(); + int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; + int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array + printArray(arr, sz); // displaying the array before sorting + heapSort(arr, sz); // calling heapsort to sort the array + printArray(arr, sz); // display array after sorting return 0; } +/** @} */ From 549daeebfdabef2e1732568d554e4cc9d5feb08f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 20 Jun 2020 17:21:37 +0000 Subject: [PATCH 08/31] formatting source-code for 8233eda8894f46785f288e72c639d201852f6096 --- sorting/merge_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index ce87c8b5c..fc6d66af5 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -114,4 +114,4 @@ int main() { delete[] arr; return 0; } -/** @} */ +/** @} */ From 38db7fdec0d7254cb559ac89a19fa884d5cd0052 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 00:06:04 +0530 Subject: [PATCH 09/31] Bug Fix heap sort [Fresh Implementation] --- sorting/heap_sort.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 405e4641d..09c4d3193 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -44,9 +44,7 @@ auto printArray = [] (int *arr, int sz) { * The heapify procedure can be thought of as building a heap from * the bottom up by successively sifting downward to establish the * heap property. - * - * @param arr array be to sorted - * @param + * */ void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) { int largest = i; From 11728ee231858c31f4398633d69a48dc5b0bba22 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 20 Jun 2020 18:34:51 +0000 Subject: [PATCH 10/31] formatting source-code for e464ddac3688834ce46c59bada5ad3fddbfa2254 --- sorting/heap_sort.cpp | 52 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 09c4d3193..ae50e1cfb 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -8,24 +8,24 @@ * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details - * heapsort is a comparison-based sorting algorithm. - * Heapsort can be thought of as an improved selection sort: - * like selection sort, heapsort divides its input into a sorted - * and an unsorted region, and it iteratively shrinks the unsorted - * region by extracting the largest element from it and inserting - * it into the sorted region. Unlike selection sort, - * heapsort does not waste time with a linear-time scan of the - * unsorted region; rather, heap sort maintains the unsorted region - * in a heap data structure to more quickly find the largest element + * heapsort is a comparison-based sorting algorithm. + * Heapsort can be thought of as an improved selection sort: + * like selection sort, heapsort divides its input into a sorted + * and an unsorted region, and it iteratively shrinks the unsorted + * region by extracting the largest element from it and inserting + * it into the sorted region. Unlike selection sort, + * heapsort does not waste time with a linear-time scan of the + * unsorted region; rather, heap sort maintains the unsorted region + * in a heap data structure to more quickly find the largest element * in each step. - * + * * Time Complexity - O(nlog(n)) * */ #include /** - * + * * Utility Lambda function to print the array after * sorting. * @@ -33,23 +33,24 @@ * @param sz size of array * */ -auto printArray = [] (int *arr, int sz) { - for (int i = 0 ; i < sz ; i++) - std::cout << arr[i] <<" "; +auto printArray = [](int *arr, int sz) { + for (int i = 0; i < sz; i++) std::cout << arr[i] << " "; std::cout << "\n"; }; /** * - * The heapify procedure can be thought of as building a heap from - * the bottom up by successively sifting downward to establish the - * heap property. - * + * The heapify procedure can be thought of as building a heap from + * the bottom up by successively sifting downward to establish the + * heap property. + * + * @param arr array be to sorted + * @param */ -void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) { +void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) { int largest = i; - int l = 2*i + 1; - int r = 2*i + 2; + int l = 2 * i + 1; + int r = 2 * i + 2; if (l < n && arr[l] > arr[largest]) largest = l; @@ -71,11 +72,10 @@ void(*heapify)(int *arr, int n, int i) = [] (int *arr, int n, int i) { * @param n size of array * */ -auto heapSort = [] (int *arr, int n) { - for (int i = n-1 ; i >= 0; i--) - heapify(arr, n, i); +auto heapSort = [](int *arr, int n) { + for (int i = n - 1; i >= 0; i--) heapify(arr, n, i); - for (int i = n-1 ; i >= 0; i--) { + for (int i = n - 1; i >= 0; i--) { std::swap(arr[0], arr[i]); heapify(arr, i, 0); } @@ -86,7 +86,7 @@ int main() { int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array printArray(arr, sz); // displaying the array before sorting - heapSort(arr, sz); // calling heapsort to sort the array + heapSort(arr, sz); // calling heapsort to sort the array printArray(arr, sz); // display array after sorting return 0; } From 86bd671c669c35b704162927401d2a7c98d3b57e Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 17:06:17 +0530 Subject: [PATCH 11/31] switched to normal functions from lambda --- sorting/heap_sort.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index ae50e1cfb..6cdb17f4a 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -33,10 +33,10 @@ * @param sz size of array * */ -auto printArray = [](int *arr, int sz) { +void printArray(int *arr, int sz) { for (int i = 0; i < sz; i++) std::cout << arr[i] << " "; std::cout << "\n"; -}; +} /** * @@ -44,10 +44,13 @@ auto printArray = [](int *arr, int sz) { * the bottom up by successively sifting downward to establish the * heap property. * - * @param arr array be to sorted - * @param + * @param arr array to be sorted + * @param n size of array + * @param i node position in Binary Tress or element position in + * Array to be compared with it's childern + * */ -void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) { +void heapify(int *arr, int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; @@ -62,7 +65,7 @@ void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) { std::swap(arr[i], arr[largest]); heapify(arr, n, largest); } -}; +} /** * heapSort lambda function utilizes heapify procedure to sort @@ -72,14 +75,14 @@ void (*heapify)(int *arr, int n, int i) = [](int *arr, int n, int i) { * @param n size of array * */ -auto heapSort = [](int *arr, int n) { +void heapSort(int *arr, int n) { for (int i = n - 1; i >= 0; i--) heapify(arr, n, i); for (int i = n - 1; i >= 0; i--) { std::swap(arr[0], arr[i]); heapify(arr, i, 0); } -}; +} /** Main function */ int main() { From 2a41d916b05681932021d4130f19af21097f385b Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 17:38:42 +0530 Subject: [PATCH 12/31] Added template and test cases --- sorting/heap_sort.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 6cdb17f4a..5eaded62a 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -23,6 +23,8 @@ * */ #include +#include +#include /** * @@ -33,7 +35,8 @@ * @param sz size of array * */ -void printArray(int *arr, int sz) { +template +void printArray(T *arr, int sz) { for (int i = 0; i < sz; i++) std::cout << arr[i] << " "; std::cout << "\n"; } @@ -50,7 +53,8 @@ void printArray(int *arr, int sz) { * Array to be compared with it's childern * */ -void heapify(int *arr, int n, int i) { +template +void heapify(T *arr, int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; @@ -75,7 +79,8 @@ void heapify(int *arr, int n, int i) { * @param n size of array * */ -void heapSort(int *arr, int n) { +template +void heapSort(T *arr, int n) { for (int i = n - 1; i >= 0; i--) heapify(arr, n, i); for (int i = n - 1; i >= 0; i--) { @@ -84,13 +89,27 @@ void heapSort(int *arr, int n) { } } -/** Main function */ -int main() { +void test() { + std::cout << "Test 1\n"; int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; int sz = sizeof(arr) / sizeof(arr[0]); // sz - size of array printArray(arr, sz); // displaying the array before sorting heapSort(arr, sz); // calling heapsort to sort the array printArray(arr, sz); // display array after sorting - return 0; + assert(std::is_sorted(arr, arr+sz)); + std::cout << "Test 1 Passed\n========================\n"; + std::cout << "Test 2\n"; + double arr2[] = {4.5, -3.6, 7.6, 0, 12.9}; + sz = sizeof(arr2) / sizeof(arr2[0]); + printArray(arr2, sz); + heapSort(arr2, sz); + printArray(arr2, sz); + assert(std::is_sorted(arr2, arr2+sz)); + std::cout << "Test 2 passed\n"; +} +/** Main function */ +int main() { + test(); + return 0; } /** @} */ From 6d88c992ed34db27b9f804e159a1a6e4cc9e59c1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 21 Jun 2020 11:37:08 +0000 Subject: [PATCH 13/31] formatting source-code for ced5dcd6c4db53ece27d22ecd1dd1c1fcce6afb0 --- sorting/heap_sort.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 5eaded62a..42bfc43b2 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -48,8 +48,8 @@ void printArray(T *arr, int sz) { * heap property. * * @param arr array to be sorted - * @param n size of array - * @param i node position in Binary Tress or element position in + * @param n size of array + * @param i node position in Binary Tress or element position in * Array to be compared with it's childern * */ From 555f4fb98f1a2646c36683362e4f72a6e40c1740 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 17:41:39 +0530 Subject: [PATCH 14/31] fixed docs --- sorting/heap_sort.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 42bfc43b2..0945c9a92 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -89,6 +89,7 @@ void heapSort(T *arr, int n) { } } +/** Test cases to test the program */ void test() { std::cout << "Test 1\n"; int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; @@ -107,6 +108,7 @@ void test() { assert(std::is_sorted(arr2, arr2+sz)); std::cout << "Test 2 passed\n"; } + /** Main function */ int main() { test(); From a56e54ca15249a322d2dd98ab83652c20e0753f1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 21 Jun 2020 12:09:45 +0000 Subject: [PATCH 15/31] formatting source-code for 7c8617fa46d41481c68ae2ae9d4f6a89ca42a4ff --- sorting/heap_sort.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 0945c9a92..52edddf17 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -22,9 +22,9 @@ * Time Complexity - O(nlog(n)) * */ -#include #include #include +#include /** * @@ -97,7 +97,7 @@ void test() { printArray(arr, sz); // displaying the array before sorting heapSort(arr, sz); // calling heapsort to sort the array printArray(arr, sz); // display array after sorting - assert(std::is_sorted(arr, arr+sz)); + assert(std::is_sorted(arr, arr + sz)); std::cout << "Test 1 Passed\n========================\n"; std::cout << "Test 2\n"; double arr2[] = {4.5, -3.6, 7.6, 0, 12.9}; @@ -105,13 +105,13 @@ void test() { printArray(arr2, sz); heapSort(arr2, sz); printArray(arr2, sz); - assert(std::is_sorted(arr2, arr2+sz)); + assert(std::is_sorted(arr2, arr2 + sz)); std::cout << "Test 2 passed\n"; } /** Main function */ int main() { - test(); - return 0; + test(); + return 0; } /** @} */ From f0ab9a0d3870ab65b054503d77cdf5188d4fdeab Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 17:50:07 +0530 Subject: [PATCH 16/31] fixed line spacing in tests --- sorting/heap_sort.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 52edddf17..6445f0f32 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -99,6 +99,7 @@ void test() { printArray(arr, sz); // display array after sorting assert(std::is_sorted(arr, arr + sz)); std::cout << "Test 1 Passed\n========================\n"; + std::cout << "Test 2\n"; double arr2[] = {4.5, -3.6, 7.6, 0, 12.9}; sz = sizeof(arr2) / sizeof(arr2[0]); From 7f4077505567f3d8c8ea275c9c7b402111769653 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sun, 21 Jun 2020 19:50:51 +0530 Subject: [PATCH 17/31] fix docs --- sorting/heap_sort.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/sorting/heap_sort.cpp b/sorting/heap_sort.cpp index 6445f0f32..ef9f87094 100644 --- a/sorting/heap_sort.cpp +++ b/sorting/heap_sort.cpp @@ -1,25 +1,23 @@ /** - * \addtogroup sorting Sorting Algorithm - * @{ * \file * \brief [Heap Sort Algorithm - * (HEAP SORT)](https://en.wikipedia.org/wiki/Heapsort) implementation + * (heap sort)](https://en.wikipedia.org/wiki/Heapsort) implementation * * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details - * heapsort is a comparison-based sorting algorithm. - * Heapsort can be thought of as an improved selection sort: - * like selection sort, heapsort divides its input into a sorted + * Heap-sort is a comparison-based sorting algorithm. + * Heap-sort can be thought of as an improved selection sort: + * like selection sort, heap sort divides its input into a sorted * and an unsorted region, and it iteratively shrinks the unsorted * region by extracting the largest element from it and inserting * it into the sorted region. Unlike selection sort, - * heapsort does not waste time with a linear-time scan of the + * heap sort does not waste time with a linear-time scan of the * unsorted region; rather, heap sort maintains the unsorted region * in a heap data structure to more quickly find the largest element * in each step. * - * Time Complexity - O(nlog(n)) + * Time Complexity - \f$O(n \log(n))\f$ * */ #include @@ -42,6 +40,9 @@ void printArray(T *arr, int sz) { } /** + * + * \addtogroup sorting Sorting Algorithm + * @{ * * The heapify procedure can be thought of as building a heap from * the bottom up by successively sifting downward to establish the @@ -72,7 +73,7 @@ void heapify(T *arr, int n, int i) { } /** - * heapSort lambda function utilizes heapify procedure to sort + * Utilizes heapify procedure to sort * the array * * @param arr array to be sorted @@ -89,7 +90,12 @@ void heapSort(T *arr, int n) { } } -/** Test cases to test the program */ +/** + * + * @} + * Test cases to test the program + * + */ void test() { std::cout << "Test 1\n"; int arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0}; @@ -115,4 +121,3 @@ int main() { test(); return 0; } -/** @} */ From c344d8f6f155cca62c183327a0ccaa6e541a4bea Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Tue, 23 Jun 2020 22:58:44 +0530 Subject: [PATCH 18/31] Multiplication result may overflow 'int' before it is converted to 'long'. --- math/primes_up_to_billion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/primes_up_to_billion.cpp b/math/primes_up_to_billion.cpp index b7377cea5..af6d34ff3 100644 --- a/math/primes_up_to_billion.cpp +++ b/math/primes_up_to_billion.cpp @@ -14,9 +14,9 @@ void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index prime[0] = '0'; // 0 is not prime prime[1] = '0'; // 1 is not prime - for (int64_t p = 2; p * p <= n; p++) { + for (long p = 2; p * p <= n; p++) { if (prime[p] == '1') { - for (int64_t i = p * p; i <= n; i += p) + for (long i = p * p; i <= n; i += p) prime[i] = '0'; // set all multiples of p to false } } From fa92a18c2e777da1b0361415d16e0fd88ae115bd Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Tue, 23 Jun 2020 23:28:13 +0530 Subject: [PATCH 19/31] fixed cpplint long -> int64 --- math/primes_up_to_billion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/primes_up_to_billion.cpp b/math/primes_up_to_billion.cpp index af6d34ff3..f13a8312b 100644 --- a/math/primes_up_to_billion.cpp +++ b/math/primes_up_to_billion.cpp @@ -14,9 +14,9 @@ void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index prime[0] = '0'; // 0 is not prime prime[1] = '0'; // 1 is not prime - for (long p = 2; p * p <= n; p++) { + for (int64 p = 2; p * p <= n; p++) { if (prime[p] == '1') { - for (long i = p * p; i <= n; i += p) + for (int64 i = p * p; i <= n; i += p) prime[i] = '0'; // set all multiples of p to false } } From 886cb520f02f8f358ff6247b3262b822b8c6b300 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Tue, 23 Jun 2020 23:36:14 +0530 Subject: [PATCH 20/31] fixed compiler error --- math/primes_up_to_billion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/primes_up_to_billion.cpp b/math/primes_up_to_billion.cpp index f13a8312b..b7377cea5 100644 --- a/math/primes_up_to_billion.cpp +++ b/math/primes_up_to_billion.cpp @@ -14,9 +14,9 @@ void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index prime[0] = '0'; // 0 is not prime prime[1] = '0'; // 1 is not prime - for (int64 p = 2; p * p <= n; p++) { + for (int64_t p = 2; p * p <= n; p++) { if (prime[p] == '1') { - for (int64 i = p * p; i <= n; i += p) + for (int64_t i = p * p; i <= n; i += p) prime[i] = '0'; // set all multiples of p to false } } From 8252816dcc6799c14fb40c6f6bf206013be3add0 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 00:12:36 +0530 Subject: [PATCH 21/31] fixed Code quality and added docs --- data_structures/disjoint_set.cpp | 62 +++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/data_structures/disjoint_set.cpp b/data_structures/disjoint_set.cpp index dd70e4cea..b7bcf3c58 100644 --- a/data_structures/disjoint_set.cpp +++ b/data_structures/disjoint_set.cpp @@ -1,3 +1,24 @@ +/** + * + * \file + * \brief [Disjoint Sets Data Structure + * (Disjoint Sets)](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) + * + * \author + * + * \details + * A disjoint set data structure (also called union find or merge find set) + * is a data structure that tracks a set of elements partitioned into a number + * of disjoint (non-overlapping) subsets. + * Some situations where disjoint sets can be used are- + * to find connected components of a graph, kruskal's algorithm for finding + * Minimum Spanning Tree etc. + * There are two operation which we perform on disjoint sets - + * 1) Union + * 2) Find + * + */ + #include #include @@ -5,16 +26,30 @@ using std::cout; using std::endl; using std::vector; -vector root, rnk; +vector root, rank; +/** + * + * Function to create a set + * @param n number of element + * + */ void CreateSet(int n) { root = vector(n + 1); - rnk = vector(n + 1, 1); + rank = vector(n + 1, 1); for (int i = 1; i <= n; ++i) { root[i] = i; } } +/** + * + * Find operation takes a number x and returns the set to which this number + * belongs to. + * @param x element of some set + * @return set to which x belongs to + * + */ int Find(int x) { if (root[x] == x) { return x; @@ -22,22 +57,39 @@ int Find(int x) { return root[x] = Find(root[x]); } +/** + * + * A utility function to check if x and y are from same set or not + * @param x element of some set + * @param y element of some set + * + */ bool InSameUnion(int x, int y) { return Find(x) == Find(y); } +/** + * + * Union operation combines two disjoint sets to make a single set + * in this union function we pass two elements and check if they are + * from different sets then combine those sets + * @param x element of some set + * @param y element of some set + * + */ void Union(int x, int y) { int a = Find(x), b = Find(y); if (a != b) { - if (rnk[a] < rnk[b]) { + if (rank[a] < rank[b]) { root[a] = b; - } else if (rnk[a] > rnk[b]) { + } else if (rank[a] > rank[b]) { root[b] = a; } else { root[a] = b; - ++rnk[b]; + ++rank[b]; } } } +/** Main function */ int main() { // tests CreateSet & Find int n = 100; From 866c23e19e733884a790de28c251c4c580bf2dc3 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 00:25:52 +0530 Subject: [PATCH 22/31] Added author name --- data_structures/disjoint_set.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/disjoint_set.cpp b/data_structures/disjoint_set.cpp index b7bcf3c58..eebb650bf 100644 --- a/data_structures/disjoint_set.cpp +++ b/data_structures/disjoint_set.cpp @@ -4,7 +4,7 @@ * \brief [Disjoint Sets Data Structure * (Disjoint Sets)](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) * - * \author + * \author [leoyang429](https://github.com/leoyang429) * * \details * A disjoint set data structure (also called union find or merge find set) From 2802dca89557750840cb89b988ce83934091e2d3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 18:43:35 +0000 Subject: [PATCH 23/31] formatting source-code for a887dc59ed8e790d479773e0ce5e24c6db110afb --- data_structures/disjoint_set.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/disjoint_set.cpp b/data_structures/disjoint_set.cpp index eebb650bf..bd4599603 100644 --- a/data_structures/disjoint_set.cpp +++ b/data_structures/disjoint_set.cpp @@ -28,8 +28,8 @@ using std::vector; vector root, rank; -/** - * +/** + * * Function to create a set * @param n number of element * @@ -57,7 +57,7 @@ int Find(int x) { return root[x] = Find(root[x]); } -/** +/** * * A utility function to check if x and y are from same set or not * @param x element of some set From 91c9c96cf486331bbe00f206b5391911d438001a Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 00:57:31 +0530 Subject: [PATCH 24/31] code quality fix deallocate resorces --- data_structures/binaryheap.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/data_structures/binaryheap.cpp b/data_structures/binaryheap.cpp index 72e56e91a..2396f2e51 100644 --- a/data_structures/binaryheap.cpp +++ b/data_structures/binaryheap.cpp @@ -13,10 +13,14 @@ class MinHeap { int heap_size; ///< Current number of elements in min heap public: - /** Constructor + /** Constructor: Builds a heap from a given array a[] of given size * \param[in] capacity initial heap capacity */ - MinHeap(int capacity); + explicit MinHeap(int cap) { + heap_size = 0; + capacity = cap; + harr = new int[cap]; + } /** to heapify a subtree with the root at given index */ @@ -44,14 +48,12 @@ class MinHeap { /** Inserts a new key 'k' */ void insertKey(int k); + + ~MinHeap() { + delete [] harr; + } }; -/** Constructor: Builds a heap from a given array a[] of given size */ -MinHeap::MinHeap(int cap) { - heap_size = 0; - capacity = cap; - harr = new int[cap]; -} // Inserts a new key 'k' void MinHeap::insertKey(int k) { From 6882b545c75136f82b5294eda0a2333b6c03ca16 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 19:29:12 +0000 Subject: [PATCH 25/31] formatting source-code for 91c9c96cf486331bbe00f206b5391911d438001a --- data_structures/binaryheap.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/data_structures/binaryheap.cpp b/data_structures/binaryheap.cpp index 2396f2e51..c3a1f5cf6 100644 --- a/data_structures/binaryheap.cpp +++ b/data_structures/binaryheap.cpp @@ -13,13 +13,13 @@ class MinHeap { int heap_size; ///< Current number of elements in min heap public: - /** Constructor: Builds a heap from a given array a[] of given size + /** Constructor: Builds a heap from a given array a[] of given size * \param[in] capacity initial heap capacity */ explicit MinHeap(int cap) { - heap_size = 0; - capacity = cap; - harr = new int[cap]; + heap_size = 0; + capacity = cap; + harr = new int[cap]; } /** to heapify a subtree with the root at given index @@ -49,12 +49,9 @@ class MinHeap { /** Inserts a new key 'k' */ void insertKey(int k); - ~MinHeap() { - delete [] harr; - } + ~MinHeap() { delete[] harr; } }; - // Inserts a new key 'k' void MinHeap::insertKey(int k) { if (heap_size == capacity) { From 2d0325699f9a832175dd376b66281bf284261c73 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 01:30:14 +0530 Subject: [PATCH 26/31] fixed code quality and added docs --- sorting/comb_sort.cpp | 83 ++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 1b0a4d706..7e62c04ff 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -1,49 +1,82 @@ -// Kind of better version of Bubble sort. -// While Bubble sort is comparering adjacent value, Combsort is using gap larger -// than 1 Best case: O(n) Worst case: O(n ^ 2) +/** + * + * \file + * \brief [Comb Sort Algorithm + * (Comb Sort)](https://en.wikipedia.org/wiki/Comb_sort) + * + * \author + * + * \details + * - A better version of bubble sort algorithm + * - Bubble sort compares adjacent values whereas comb sort uses gap larger + * than 1 + * - Best case Time complexity O(n) + * Worst case Time complexity O(n^2) + * + */ #include #include -int a[100005]; -int n; +/** + * + * Find the next gap by shrinking the current gap by shrink factor of 1.3 + * @param gap current gap + * @return new gap + * + */ +int FindNextGap(int gap) { + gap = (gap * 10) / 13; -int FindNextGap(int x) { - x = (x * 10) / 13; - - return std::max(1, x); + return std::max(1, gap); } -void CombSort(int a[], int l, int r) { - // Init gap - int gap = n; +/** Function to sort array + * + * @param arr array to be sorted + * @param l start index of array + * @param r end index of array + * + */ +void CombSort(int *arr, int l, int r) { + /** + * + * initial gap will be maximum and the maximum possible value is + * the size of the array that is n and which is equal to r in this + * case so to avoid passing an extra parameter n that is the size of + * the array we are using r to initialize the initial gap. + * + */ + int gap = r; - // Initialize swapped as true to make sure that loop runs + /// Initialize swapped as true to make sure that loop runs bool swapped = true; - // Keep running until gap = 1 or none elements were swapped + /// Keep running until gap = 1 or none elements were swapped while (gap != 1 || swapped) { - // Find next gap + /// Find next gap gap = FindNextGap(gap); swapped = false; - // Compare all elements with current gap + /// Compare all elements with current gap for (int i = l; i <= r - gap; ++i) { - if (a[i] > a[i + gap]) { - std::swap(a[i], a[i + gap]); + if (arr[i] > arr[i + gap]) { + std::swap(arr[i], arr[i + gap]); swapped = true; } } } } +/** Main function */ int main() { - std::cin >> n; - for (int i = 1; i <= n; ++i) std::cin >> a[i]; - - CombSort(a, 1, n); - - for (int i = 1; i <= n; ++i) std::cout << a[i] << ' '; - return 0; + int n; + std::cin >> n; + int *arr = new int[n]; + for (int i = 0; i < n; ++i) std::cin >> arr[i]; + CombSort(arr, 0, n); + for (int i = 0; i < n; ++i) std::cout << arr[i] << ' '; + delete [] arr; + return 0; } From 33b5033adcb1e5551dfd3c6ca33ffe597316301b Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:01:26 +0000 Subject: [PATCH 27/31] formatting source-code for f030a69288662f1d282a9d724501990830c5bda7 --- sorting/comb_sort.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 7e62c04ff..d44269814 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -71,12 +71,12 @@ void CombSort(int *arr, int l, int r) { /** Main function */ int main() { - int n; - std::cin >> n; - int *arr = new int[n]; - for (int i = 0; i < n; ++i) std::cin >> arr[i]; - CombSort(arr, 0, n); - for (int i = 0; i < n; ++i) std::cout << arr[i] << ' '; - delete [] arr; - return 0; + int n; + std::cin >> n; + int *arr = new int[n]; + for (int i = 0; i < n; ++i) std::cin >> arr[i]; + CombSort(arr, 0, n); + for (int i = 0; i < n; ++i) std::cout << arr[i] << ' '; + delete[] arr; + return 0; } From 92d2bf012ab4687bb973cc52d146740bf72ef9a3 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 01:49:33 +0530 Subject: [PATCH 28/31] Added tests for comb_sort.cpp --- sorting/comb_sort.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 7e62c04ff..00b12fcb2 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -17,6 +17,7 @@ #include #include +#include /** * @@ -69,8 +70,26 @@ void CombSort(int *arr, int l, int r) { } } +void tests() { + /// Test 1 + int arr1[10] = {34, 56, 6, 23, 76, 34, 76, 343, 4, 76}; + CombSort(arr1, 0, 10); + assert(std::is_sorted(arr1, arr1 + 10)); + std::cout << "Test 1 passed\n"; + + /// Test 2 + int arr2[8] = {-6, 56, -45, 56, 0, -1, 8, 8}; + CombSort(arr2, 0, 8); + assert(std::is_sorted(arr2, arr2 + 8)); + std::cout << "Test 2 Passed\n"; +} + /** Main function */ int main() { + /// Running predefined tests + tests(); + + /// For user interaction int n; std::cin >> n; int *arr = new int[n]; From 48a163e51ab53a73c9edbb02633e6a3673865cea Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:22:30 +0000 Subject: [PATCH 29/31] formatting source-code for 3a7a266ada62816c9602bfd9d5b632b80a169617 --- sorting/comb_sort.cpp | 44 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 00b12fcb2..e99ae9514 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -16,8 +16,8 @@ */ #include -#include #include +#include /** * @@ -71,31 +71,31 @@ void CombSort(int *arr, int l, int r) { } void tests() { - /// Test 1 - int arr1[10] = {34, 56, 6, 23, 76, 34, 76, 343, 4, 76}; - CombSort(arr1, 0, 10); - assert(std::is_sorted(arr1, arr1 + 10)); - std::cout << "Test 1 passed\n"; + /// Test 1 + int arr1[10] = {34, 56, 6, 23, 76, 34, 76, 343, 4, 76}; + CombSort(arr1, 0, 10); + assert(std::is_sorted(arr1, arr1 + 10)); + std::cout << "Test 1 passed\n"; - /// Test 2 - int arr2[8] = {-6, 56, -45, 56, 0, -1, 8, 8}; - CombSort(arr2, 0, 8); - assert(std::is_sorted(arr2, arr2 + 8)); - std::cout << "Test 2 Passed\n"; + /// Test 2 + int arr2[8] = {-6, 56, -45, 56, 0, -1, 8, 8}; + CombSort(arr2, 0, 8); + assert(std::is_sorted(arr2, arr2 + 8)); + std::cout << "Test 2 Passed\n"; } /** Main function */ int main() { - /// Running predefined tests - tests(); + /// Running predefined tests + tests(); - /// For user interaction - int n; - std::cin >> n; - int *arr = new int[n]; - for (int i = 0; i < n; ++i) std::cin >> arr[i]; - CombSort(arr, 0, n); - for (int i = 0; i < n; ++i) std::cout << arr[i] << ' '; - delete [] arr; - return 0; + /// For user interaction + int n; + std::cin >> n; + int *arr = new int[n]; + for (int i = 0; i < n; ++i) std::cin >> arr[i]; + CombSort(arr, 0, n); + for (int i = 0; i < n; ++i) std::cout << arr[i] << ' '; + delete[] arr; + return 0; } From 2464d438f9a3a579fd3ed97589d4ccb9c353c509 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 02:06:52 +0530 Subject: [PATCH 30/31] deleted line 27 --- data_structures/binaryheap.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/binaryheap.cpp b/data_structures/binaryheap.cpp index c3a1f5cf6..21da4845b 100644 --- a/data_structures/binaryheap.cpp +++ b/data_structures/binaryheap.cpp @@ -24,7 +24,6 @@ class MinHeap { /** to heapify a subtree with the root at given index */ - void MinHeapify(int); int parent(int i) { return (i - 1) / 2; } From 62aafd1c5079260b649e6cae5f8133d9e8287b36 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 24 Jun 2020 02:22:20 +0530 Subject: [PATCH 31/31] reverted line 27 --- data_structures/binaryheap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binaryheap.cpp b/data_structures/binaryheap.cpp index 21da4845b..483bd7c02 100644 --- a/data_structures/binaryheap.cpp +++ b/data_structures/binaryheap.cpp @@ -22,8 +22,8 @@ class MinHeap { harr = new int[cap]; } - /** to heapify a subtree with the root at given index - */ + /** to heapify a subtree with the root at given index */ + void MinHeapify(int); int parent(int i) { return (i - 1) / 2; }