From a6319c5f43d019a23a8eb3ffa48f6b8a30ff85a4 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 08:48:09 -0400 Subject: [PATCH 01/28] newline character at EOF of gitignore (cherry picked from commit 614ea8eb5841199d86c40414935e68e10bc3b918) --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 12318c6cd..5275088d0 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,4 @@ a.out *.out *.app -build/ \ No newline at end of file +build/ From 36b4d59d5914f1c35f09f82cab65514a48166fa7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:05:29 -0400 Subject: [PATCH 02/28] `rand_r` is non-portable and obsolete (cherry picked from commit 0ad756f8609efdb5fd3d27508fa692c738fc1fc7) --- math/fast_power.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 4f6e02081..5dd724085 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -50,30 +50,28 @@ double fast_power_linear(int64_t a, int64_t b) { } int main() { - std::srand(time(NULL)); + std::srand(std::time(nullptr)); std::ios_base::sync_with_stdio(false); std::cout << "Testing..." << std::endl; for (int i = 0; i < 20; i++) { - unsigned int *rand1, *rand2; - int a = rand_r(rand1) % 20 - 10; - int b = rand_r(rand2) % 20 - 10; + int a = std::rand() % 20 - 10; + int b = std::rand() % 20 - 10; std::cout << std::endl << "Calculating " << a << "^" << b << std::endl; assert(fast_power_recursive(a, b) == std::pow(a, b)); assert(fast_power_linear(a, b) == std::pow(a, b)); - std::cout << "------ " << a << "^" << b << " = "<< - fast_power_recursive(a, b) << std::endl; + std::cout << "------ " << a << "^" << b << " = " + << fast_power_recursive(a, b) << std::endl; } int64_t a, b; std::cin >> a >> b; - std::cout << a << "^" << b << " = "<< - fast_power_recursive(a, b) << std::endl; + std::cout << a << "^" << b << " = " << fast_power_recursive(a, b) + << std::endl; - std::cout << a << "^" << b << " = "<< - fast_power_linear(a, b) << std::endl; + std::cout << a << "^" << b << " = " << fast_power_linear(a, b) << std::endl; return 0; } From 05ec7bed7282997e36dec1f7dd5273e85e46834c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:05:44 -0400 Subject: [PATCH 03/28] improved documentation (cherry picked from commit 139964d32563b3c92e0651bf46350e107391c5a5) --- math/fast_power.cpp | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 5dd724085..0ffbcd40d 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -1,22 +1,29 @@ -#include -#include -#include -#include -#include -#include - -/* - Program that computes a^b in O(logN) time. +/** + * @file + Program that computes \f$a^b\f$ in \f$O(logN)\f$ time. It is based on formula that: - case1) if b is even: a^b = a^(b/2) * a^(b/2) = (a^(b/2))ˆ2 - case2) if b is odd: a^b = a^((b-1)/2) * a^((b-1)/2) * a = (a^((b-1)/2))^2 * a - We can compute a^b recursively using above algorithm. + 1. if \f$b\f$ is even: \f$a^b = a^\frac{b}{2} \cdot a^\frac{b}{2} = + {a^\frac{b}{2}}^2\f$ + 2. if \f$b\f$ is odd: \f$a^b = a^\frac{b-1}{2} \cdot + a^\frac{b-1}{2} \cdot a = {a^\frac{b-1}{2}}^2 \cdot a\f$ + + We can compute \f$a^b\f$ + recursively using above algorithm. */ +#include +#include +#include +#include +#include +#include + +/** + * algorithm implementation for \f$a^b\f$ + */ double fast_power_recursive(int64_t a, int64_t b) { // negative power. a^b = 1 / (a^-b) - if (b < 0) - return 1.0 / fast_power_recursive(a, -b); + if (b < 0) return 1.0 / fast_power_recursive(a, -b); if (b == 0) return 1; int64_t bottom = fast_power_recursive(a, b >> 1); @@ -31,14 +38,13 @@ double fast_power_recursive(int64_t a, int64_t b) { return result; } -/* +/** Same algorithm with little different formula. It still calculates in O(logN) */ double fast_power_linear(int64_t a, int64_t b) { // negative power. a^b = 1 / (a^-b) - if (b < 0) - return 1.0 / fast_power_linear(a, -b); + if (b < 0) return 1.0 / fast_power_linear(a, -b); double result = 1; while (b) { From 5fddd6c7cd3d8deb2d8e135c3fe3d11586244957 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:10:28 -0400 Subject: [PATCH 04/28] cin accepts only one variable at a time & fixed cpplint (cherry picked from commit 2def9abcc2306ab569687f85f1802c4e5493d244) --- others/sparse_matrix.cpp | 65 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index 1861163f1..de4759a3e 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -1,41 +1,40 @@ -/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2, -where m and n are the dimensions of the matrix.*/ +/** @file + * A sparse matrix is a matrix which has number of zeroes greater than + * \f$\frac{m*n}{2}\f$, where m and n are the dimensions of the matrix. + */ + #include -using namespace std; -int main() -{ - int m, n; - int counterZeros = 0; - cout << "Enter dimensions of matrix (seperated with space): "; - cin >> m >> n; - int a[m][n]; - cout << "Enter matrix elements:"; - cout << "\n"; +int main() { + int m, n; + int counterZeros = 0; - // reads the matrix from stdin - for (int i = 0; i < m; i++) - { - for (int j = 0; j < n; j++) - { - cout << "element? "; - cin >> a[i][j]; + std::cout << "Enter dimensions of matrix (seperated with space): "; + std::cin >> m; + std::cin >> n; + + int a[m][n]; + std::cout << "Enter matrix elements:"; + std::cout << "\n"; + + // reads the matrix from stdin + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + std::cout << "element? "; + std::cin >> a[i][j]; + } } - } - // counts the zero's - for (int i = 0; i < m; i++) - { - for (int j = 0; j < n; j++) - { - if (a[i][j] == 0) - counterZeros++; //Counting number of zeroes + // counts the zero's + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (a[i][j] == 0) counterZeros++; // Counting number of zeroes + } } - } - // makes sure the matrix is a sparse matrix - if (counterZeros > ((m * n) / 2)) //Checking for sparse matrix - cout << "Sparse matrix"; - else - cout << "Not a sparse matrix"; + // makes sure the matrix is a sparse matrix + if (counterZeros > ((m * n) / 2)) // Checking for sparse matrix + std::cout << "Sparse matrix"; + else + std::cout << "Not a sparse matrix"; } From 5f8b270992e4b6f344aa50b78a92236c8e48a48b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:25:42 -0400 Subject: [PATCH 05/28] file rename (cherry picked from commit d6d328c8c9a760b86e61b6e3e491a6a4416926db) --- others/{Palindromeofnumber.cpp => palindrome_of_number.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename others/{Palindromeofnumber.cpp => palindrome_of_number.cpp} (100%) diff --git a/others/Palindromeofnumber.cpp b/others/palindrome_of_number.cpp similarity index 100% rename from others/Palindromeofnumber.cpp rename to others/palindrome_of_number.cpp From 59be834836c839db7fba0ab852193252e6fa3eeb Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:26:40 -0400 Subject: [PATCH 06/28] fixed lint (cherry picked from commit fb3a8091f9492ccac9b66572612e39e394eed4ad) --- others/palindrome_of_number.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp index 647803997..dda872882 100644 --- a/others/palindrome_of_number.cpp +++ b/others/palindrome_of_number.cpp @@ -1,23 +1,20 @@ -#include #include +#include -using namespace std; +int main() { + int num; + std::cout << "Enter number = "; + std::cin >> num; -int main() -{ - int num; - cout << "Enter number = "; - cin >> num; + std::string s1 = std::to_string(num); + std::string s2 = s1; - string s1 = to_string(num); - string s2 = s1; + reverse(s1.begin(), s1.end()); - reverse(s1.begin(), s1.end()); + if (s1 == s2) + std::cout << "true"; + else + std::cout << "false"; - if (s1 == s2) - cout << "true"; - else - cout << "false"; - - return 0; + return 0; } From 48473295275ad3b1911365f0216249d44e3afe8b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:32:37 -0400 Subject: [PATCH 07/28] fixed windows build error (cherry picked from commit 4b25222d430a3be8b2072cd671df7513412c8a5c) --- .../gaussian_elimination.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/computer_oriented_statistical_methods/gaussian_elimination.cpp b/computer_oriented_statistical_methods/gaussian_elimination.cpp index ecfbfacc9..0b8bb693d 100644 --- a/computer_oriented_statistical_methods/gaussian_elimination.cpp +++ b/computer_oriented_statistical_methods/gaussian_elimination.cpp @@ -6,7 +6,11 @@ int main() { std::cout << "Matrix size: "; std::cin >> mat_size; - double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1]; + double **mat = new double *[mat_size + 1], **x = new double *[mat_size]; + for (i = 0; i <= mat_size; i++) { + mat[i] = new double[mat_size + 1]; + if (i < mat_size) x[i] = new double[mat_size + 1]; + } std::cout << std::endl << "Enter value of the matrix: " << std::endl; for (i = 0; i < mat_size; i++) { @@ -49,5 +53,13 @@ int main() { std::cout << "x" << i << "= " << x[i][i] << std::endl; } + + for (i = 0; i <= mat_size; i++) { + delete[] mat[i]; + if (i < mat_size) delete[] x[i]; + } + delete[] mat; + delete[] x; + return 0; } From 8cb310a9fce0c50101487c4f559a6db3d79290c0 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:35:25 -0400 Subject: [PATCH 08/28] removed in-favor of chronos library of c++11 (cherry picked from commit 899be6a1f17e4f1bd1e7fee6375432767757cc0b) --- others/measure_time_elapsed.cpp | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 others/measure_time_elapsed.cpp diff --git a/others/measure_time_elapsed.cpp b/others/measure_time_elapsed.cpp deleted file mode 100644 index d0830ab79..000000000 --- a/others/measure_time_elapsed.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// To calculate the time taken by a code to execute -#include -#include - -__int64_t getTimeInMicroseconds() { - struct timeval start; - gettimeofday(&start, NULL); - return start.tv_sec * 1000000 + start.tv_usec; -} - -// write function sample(args) - -int main() { - // write code - __int64_t starttime = getTimeInMicroseconds(); - // sample(args) function run - // Any other functions (if present) run - std::cout << getTimeInMicroseconds() - starttime; -} From 64c8bf38bdb214c9a0eb1e3c6c2f0245e9dcad78 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:37:36 -0400 Subject: [PATCH 09/28] fixed dynamic array (cherry picked from commit dd20bf94427392ea0baa428ecf2587d6a8a7a131) --- others/sparse_matrix.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index de4759a3e..d9878fda4 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -13,7 +13,9 @@ int main() { std::cin >> m; std::cin >> n; - int a[m][n]; + int **a = new int *[m]; + for (int i = 0; i < m; i++) a[i] = new int[n]; + std::cout << "Enter matrix elements:"; std::cout << "\n"; @@ -37,4 +39,8 @@ int main() { std::cout << "Sparse matrix"; else std::cout << "Not a sparse matrix"; + + for (int i = 0; i < m; i++) delete[] a[i]; + delete[] a; + return 0; } From a15b5c5a9ba954c112e8e599e7885d9afb297ac0 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 10:06:01 -0400 Subject: [PATCH 10/28] MSVC does not know cstring-use string-for toString (cherry picked from commit fe9d2b9bc66ee9f2e5112ced6b6d5aab7d5e5f05) --- others/palindrome_of_number.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp index dda872882..621b09ab6 100644 --- a/others/palindrome_of_number.cpp +++ b/others/palindrome_of_number.cpp @@ -1,6 +1,13 @@ #include #include +#ifdef _MSC_VER +// Required to compile std::toString function using MSVC +#include +#else +#include +#endif + int main() { int num; std::cout << "Enter number = "; From cfe9142c92e7cabc763b0c931f6bec5548c46c0a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 14:18:48 +0000 Subject: [PATCH 11/28] updating DIRECTORY.md --- DIRECTORY.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e163d414..47842b9ca 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -141,8 +141,7 @@ * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/gcd_of_n_numbers.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) - * [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp) - * [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp) + * [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp) * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp) * [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp) * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp) From e4084e77a81a4124ad58b294cc2618cb917ec59e Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 11:33:57 -0400 Subject: [PATCH 12/28] fix CPPLINT in sorting folder (cherry picked from commit 7efa52e0672749b9915d8af03545f66fd6d5848e) --- sorting/BeadSort.cpp | 63 --- sorting/BitonicSort.cpp | 76 ---- sorting/Bubble Sort.cpp | 83 ---- sorting/CocktailSelectionSort.cpp | 109 ----- sorting/NumericStringSort.cpp | 62 --- sorting/bead_sort.cpp | 54 +++ sorting/bitonic_sort.cpp | 63 +++ sorting/bubble_sort.cpp | 83 ++++ sorting/{bucketSort.cpp => bucket_sort.cpp} | 0 sorting/cocktail_selection_sort.cpp | 101 +++++ sorting/{combsort.cpp => comb_sort.cpp} | 0 ...ortString.cpp => counting_sort_string.cpp} | 0 sorting/doxy.txt | 374 ------------------ ...{Insertion Sort.cpp => insertion_sort.cpp} | 0 sorting/makefile | 11 - sorting/{Merge Sort.cpp => merge_sort.cpp} | 0 sorting/non_recursive_merge_sort.cpp | 38 +- sorting/numeric_string_sort.cpp | 54 +++ .../{OddEven Sort.cpp => odd_even_sort.cpp} | 0 sorting/{Quick Sort.cpp => quick_sort.cpp} | 0 sorting/{Radix Sort.cpp => radix_sort.cpp} | 0 ...{Selection Sort.cpp => selection_sort.cpp} | 0 sorting/{Shell Sort.cpp => shell_sort.cpp} | 0 sorting/{Slow Sort.cpp => slow_sort.cpp} | 0 sorting/{Tim Sort.cpp => tim_sort.cpp} | 0 25 files changed, 372 insertions(+), 799 deletions(-) delete mode 100644 sorting/BeadSort.cpp delete mode 100644 sorting/BitonicSort.cpp delete mode 100644 sorting/Bubble Sort.cpp delete mode 100644 sorting/CocktailSelectionSort.cpp delete mode 100644 sorting/NumericStringSort.cpp create mode 100644 sorting/bead_sort.cpp create mode 100644 sorting/bitonic_sort.cpp create mode 100644 sorting/bubble_sort.cpp rename sorting/{bucketSort.cpp => bucket_sort.cpp} (100%) create mode 100644 sorting/cocktail_selection_sort.cpp rename sorting/{combsort.cpp => comb_sort.cpp} (100%) rename sorting/{CountingSortString.cpp => counting_sort_string.cpp} (100%) delete mode 100644 sorting/doxy.txt rename sorting/{Insertion Sort.cpp => insertion_sort.cpp} (100%) delete mode 100644 sorting/makefile rename sorting/{Merge Sort.cpp => merge_sort.cpp} (100%) create mode 100644 sorting/numeric_string_sort.cpp rename sorting/{OddEven Sort.cpp => odd_even_sort.cpp} (100%) rename sorting/{Quick Sort.cpp => quick_sort.cpp} (100%) rename sorting/{Radix Sort.cpp => radix_sort.cpp} (100%) rename sorting/{Selection Sort.cpp => selection_sort.cpp} (100%) rename sorting/{Shell Sort.cpp => shell_sort.cpp} (100%) rename sorting/{Slow Sort.cpp => slow_sort.cpp} (100%) rename sorting/{Tim Sort.cpp => tim_sort.cpp} (100%) diff --git a/sorting/BeadSort.cpp b/sorting/BeadSort.cpp deleted file mode 100644 index c8e27b250..000000000 --- a/sorting/BeadSort.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// C++ program to implement gravity/bead sort -#include -#include -using namespace std; - -#define BEAD(i, j) beads[i * max + j] - -// function to perform the above algorithm -void beadSort(int *a, int len) -{ - // Find the maximum element - int max = a[0]; - for (int i = 1; i < len; i++) - if (a[i] > max) - max = a[i]; - - // allocating memory - unsigned char beads[max*len]; - memset(beads, 0, sizeof(beads)); - - // mark the beads - for (int i = 0; i < len; i++) - for (int j = 0; j < a[i]; j++) - BEAD(i, j) = 1; - - for (int j = 0; j < max; j++) - { - // count how many beads are on each post - int sum = 0; - for (int i=0; i < len; i++) - { - sum += BEAD(i, j); - BEAD(i, j) = 0; - } - - // Move beads down - for (int i = len - sum; i < len; i++) - BEAD(i, j) = 1; - } - - // Put sorted values in array using beads - for (int i = 0; i < len; i++) - { - int j; - for (j = 0; j < max && BEAD(i, j); j++); - - a[i] = j; - } -} - -// driver function to test the algorithm -int main() -{ - int a[] = {5, 3, 1, 7, 4, 1, 1, 20}; - int len = sizeof(a)/sizeof(a[0]); - - beadSort(a, len); - - for (int i = 0; i < len; i++) - printf("%d ", a[i]); - - return 0; -} diff --git a/sorting/BitonicSort.cpp b/sorting/BitonicSort.cpp deleted file mode 100644 index 6ad2489d2..000000000 --- a/sorting/BitonicSort.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// Source : https://www.geeksforgeeks.org/bitonic-sort/ - -/* C++ Program for Bitonic Sort. Note that this program - works only when size of input is a power of 2. */ - -#include -#include -using namespace std; - -/*The parameter dir indicates the sorting direction, ASCENDING - or DESCENDING; if (a[i] > a[j]) agrees with the direction, - then a[i] and a[j] are interchanged.*/ -void compAndSwap(int a[], int i, int j, int dir) -{ - if (dir == (a[i] > a[j])) - swap(a[i], a[j]); -} - -/*It recursively sorts a bitonic sequence in ascending order, - if dir = 1, and in descending order otherwise (means dir=0). - The sequence to be sorted starts at index position low, - the parameter cnt is the number of elements to be sorted.*/ -void bitonicMerge(int a[], int low, int cnt, int dir) -{ - if (cnt > 1) - { - int k = cnt / 2; - for (int i = low; i < low + k; i++) - compAndSwap(a, i, i + k, dir); - bitonicMerge(a, low, k, dir); - bitonicMerge(a, low + k, k, dir); - } -} - -/* This function first produces a bitonic sequence by recursively - sorting its two halves in opposite sorting orders, and then - calls bitonicMerge to make them in the same order */ -void bitonicSort(int a[], int low, int cnt, int dir) -{ - if (cnt > 1) - { - int k = cnt / 2; - - // sort in ascending order since dir here is 1 - bitonicSort(a, low, k, 1); - - // sort in descending order since dir here is 0 - bitonicSort(a, low + k, k, 0); - - // Will merge wole sequence in ascending order - // since dir=1. - bitonicMerge(a, low, cnt, dir); - } -} - -/* Caller of bitonicSort for sorting the entire array of - length N in ASCENDING order */ -void sort(int a[], int N, int up) -{ - bitonicSort(a, 0, N, up); -} - -// Driver code -int main() -{ - int a[] = {3, 7, 4, 8, 6, 2, 1, 5}; - int N = sizeof(a) / sizeof(a[0]); - - int up = 1; // means sort in ascending order - sort(a, N, up); - - printf("Sorted array: \n"); - for (int i = 0; i < N; i++) - printf("%d ", a[i]); - return 0; -} diff --git a/sorting/Bubble Sort.cpp b/sorting/Bubble Sort.cpp deleted file mode 100644 index b160ac479..000000000 --- a/sorting/Bubble Sort.cpp +++ /dev/null @@ -1,83 +0,0 @@ -//Bubble Sort - -#include -#include -using namespace std; - -int main() -{ - int n; - short swap_check = 1; - cout << "Enter the amount of numbers to sort: "; - cin >> n; - vector numbers; - cout << "Enter " << n << " numbers: "; - int num; - - //Input - for (int i = 0; i < n; i++) - { - cin >> num; - numbers.push_back(num); - } - - //Bubble Sorting - for (int i = 0; (i < n) && (swap_check == 1); i++) - { - swap_check = 0; - for (int j = 0; j < n - 1 - i; j++) - { - if (numbers[j] > numbers[j + 1]) - { - swap_check = 1; - swap(numbers[j], numbers[j + 1]);// by changing swap location. I mean, j. If the number is greater than j + 1, then it means the location. - } - } - } - - //Output - cout << "\nSorted Array : "; - for (int i = 0; i < numbers.size(); i++) - { - if (i != numbers.size() - 1) - { - cout << numbers[i] << ", "; - } - else - { - cout << numbers[i] << endl; - } - } - return 0; -} - -/*The working principle of the Bubble sort algorithm: - -Bubble sort algorithm is the bubble sorting algorithm. The most important reason for calling the bubble is that the largest number is thrown at the end of this algorithm. -This is all about the logic. -In each iteration, the largest number is expired and when iterations are completed, the sorting takes place. - -What is Swap? - -Swap in the software means that two variables are displaced. -An additional variable is required for this operation. x = 5, y = 10. -We want x = 10, y = 5. Here we create the most variable to do it. - -int z; -z = x; -x = y; -y = z; - -The above process is a typical displacement process. -When x assigns the value to x, the old value of x is lost. -That's why we created a variable z to create the first value of the value of x, and finally, we have assigned to y. - -Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) - -Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you remember Big O Notation, we were calculating the complexity of the algorithms in the nested loops. -The n * (n - 1) product gives us O (n²) performance. In the worst case all the steps of the cycle will occur. -Bubble Sort (Avarage Case) Performance. Bubble Sort is not an optimal algorithm. -in average, O (n²) performance is taken. -Bubble Sort Best Case Performance. O (n). -However, you can't get the best status in the code we shared above. This happens on the optimized bubble sort algorithm. It's right down there. -* / diff --git a/sorting/CocktailSelectionSort.cpp b/sorting/CocktailSelectionSort.cpp deleted file mode 100644 index cac6a3618..000000000 --- a/sorting/CocktailSelectionSort.cpp +++ /dev/null @@ -1,109 +0,0 @@ -//Returns Sorted elements after performing Cocktail Selection Sort -//It is a Sorting algorithm which chooses the minimum and maximum element in an array simultaneously, -//and swaps it with the lowest and highest available position iteratively or recursively - -#include -using namespace std; - -//Iterative Version - -void CocktailSelectionSort(vector &vec, int low, int high) -{ - while (low <= high) - { - int minimum = vec[low]; - int minimumindex = low; - int maximum = vec[high]; - int maximumindex = high; - - for (int i = low; i <= high; i++) - { - if (vec[i] >= maximum) - { - maximum = vec[i]; - maximumindex = i; - } - if (vec[i] <= minimum) - { - minimum = vec[i]; - minimumindex = i; - } - } - if (low != maximumindex || high != minimumindex) - { - swap(vec[low], vec[minimumindex]); - swap(vec[high], vec[maximumindex]); - } - else - { - swap(vec[low], vec[high]); - } - - low++; - high--; - } -} - -//Recursive Version - -void CocktailSelectionSort(vector &vec, int low, int high) -{ - - if (low >= high) - return; - - int minimum = vec[low]; - int minimumindex = low; - int maximum = vec[high]; - int maximumindex = high; - - for (int i = low; i <= high; i++) - { - if (vec[i] >= maximum) - { - maximum = vec[i]; - maximumindex = i; - } - if (vec[i] <= minimum) - { - minimum = vec[i]; - minimumindex = i; - } - } - if (low != maximumindex || high != minimumindex) - { - swap(vec[low], vec[minimumindex]); - swap(vec[high], vec[maximumindex]); - } - else - { - swap(vec[low], vec[high]); - } - - CocktailSelectionSort(vec, low + 1, high - 1); -} - -//main function, select any one of iterative or recursive version - -int main() -{ - - int n; - cout << "Enter number of elements\n"; - cin >> n; - std::vector v(n); - cout << "Enter all the elements\n"; - for (int i = 0; i < n; ++i) - { - cin >> v[i]; - } - - CocktailSelectionSort(v, 0, n - 1); - cout << "Sorted elements are\n"; - for (int i = 0; i < n; ++i) - { - cout << v[i] << " "; - } - - return 0; -} diff --git a/sorting/NumericStringSort.cpp b/sorting/NumericStringSort.cpp deleted file mode 100644 index 02f6964ba..000000000 --- a/sorting/NumericStringSort.cpp +++ /dev/null @@ -1,62 +0,0 @@ -//Using general algorithms to sort a collection of strings results in alphanumeric sort. -//If it is a numeric string, it leads to unnatural sorting - -//eg, an array of strings 1,10,100,2,20,200,3,30,300 -//would be sorted in that same order by using conventional sorting, -//even though we know the correct sorting order is 1,2,3,10,20,30,100,200,300 - -//This Programme uses a comparator to sort the array in Numerical order instead of Alphanumeric order - -#include -#include -#include -using namespace std; - -bool NumericSort(string a, string b) -{ - while (a[0] == '0') - { - a.erase(a.begin()); - } - while (b[0] == '0') - { - b.erase(b.begin()); - } - int n = a.length(); - int m = b.length(); - if (n == m) - return a < b; - return n < m; -} - -int main() -{ - - int n; - cout << "Enter number of elements to be sorted Numerically\n"; - cin >> n; - - vector v(n); - cout << "Enter the string of Numbers\n"; - for (int i = 0; i < n; i++) - { - cin >> v[i]; - } - - sort(v.begin(), v.end()); - cout << "Elements sorted normally \n"; - for (int i = 0; i < n; i++) - { - cout << v[i] << " "; - } - cout << "\n"; - - sort(v.begin(), v.end(), NumericSort); - cout << "Elements sorted Numerically \n"; - for (int i = 0; i < n; i++) - { - cout << v[i] << " "; - } - - return 0; -} diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp new file mode 100644 index 000000000..12b7d4d32 --- /dev/null +++ b/sorting/bead_sort.cpp @@ -0,0 +1,54 @@ +// C++ program to implement gravity/bead sort +#include +#include + +#define BEAD(i, j) beads[i * max + j] + +// function to perform the above algorithm +void beadSort(int *a, int len) { + // Find the maximum element + int max = a[0]; + for (int i = 1; i < len; i++) + if (a[i] > max) max = a[i]; + + // allocating memory + unsigned char beads[max * len]; + memset(beads, 0, sizeof(beads)); + + // mark the beads + for (int i = 0; i < len; i++) + for (int j = 0; j < a[i]; j++) BEAD(i, j) = 1; + + for (int j = 0; j < max; j++) { + // count how many beads are on each post + int sum = 0; + for (int i = 0; i < len; i++) { + sum += BEAD(i, j); + BEAD(i, j) = 0; + } + + // Move beads down + for (int i = len - sum; i < len; i++) BEAD(i, j) = 1; + } + + // Put sorted values in array using beads + for (int i = 0; i < len; i++) { + int j; + for (j = 0; j < max && BEAD(i, j); j++) + ; + + a[i] = j; + } +} + +// driver function to test the algorithm +int main() { + int a[] = {5, 3, 1, 7, 4, 1, 1, 20}; + int len = sizeof(a) / sizeof(a[0]); + + beadSort(a, len); + + for (int i = 0; i < len; i++) printf("%d ", a[i]); + + return 0; +} diff --git a/sorting/bitonic_sort.cpp b/sorting/bitonic_sort.cpp new file mode 100644 index 000000000..4d0981056 --- /dev/null +++ b/sorting/bitonic_sort.cpp @@ -0,0 +1,63 @@ +// Source : https://www.geeksforgeeks.org/bitonic-sort/ + +/* C++ Program for Bitonic Sort. Note that this program + works only when size of input is a power of 2. */ + +#include +#include + +/*The parameter dir indicates the sorting direction, ASCENDING + or DESCENDING; if (a[i] > a[j]) agrees with the direction, + then a[i] and a[j] are interchanged.*/ +void compAndSwap(int a[], int i, int j, int dir) { + if (dir == (a[i] > a[j])) std::swap(a[i], a[j]); +} + +/*It recursively sorts a bitonic sequence in ascending order, + if dir = 1, and in descending order otherwise (means dir=0). + The sequence to be sorted starts at index position low, + the parameter cnt is the number of elements to be sorted.*/ +void bitonicMerge(int a[], int low, int cnt, int dir) { + if (cnt > 1) { + int k = cnt / 2; + for (int i = low; i < low + k; i++) compAndSwap(a, i, i + k, dir); + bitonicMerge(a, low, k, dir); + bitonicMerge(a, low + k, k, dir); + } +} + +/* This function first produces a bitonic sequence by recursively + sorting its two halves in opposite sorting orders, and then + calls bitonicMerge to make them in the same order */ +void bitonicSort(int a[], int low, int cnt, int dir) { + if (cnt > 1) { + int k = cnt / 2; + + // sort in ascending order since dir here is 1 + bitonicSort(a, low, k, 1); + + // sort in descending order since dir here is 0 + bitonicSort(a, low + k, k, 0); + + // Will merge wole sequence in ascending order + // since dir=1. + bitonicMerge(a, low, cnt, dir); + } +} + +/* Caller of bitonicSort for sorting the entire array of + length N in ASCENDING order */ +void sort(int a[], int N, int up) { bitonicSort(a, 0, N, up); } + +// Driver code +int main() { + int a[] = {3, 7, 4, 8, 6, 2, 1, 5}; + int N = sizeof(a) / sizeof(a[0]); + + int up = 1; // means sort in ascending order + sort(a, N, up); + + std::cout << "Sorted array: \n"; + for (int i = 0; i < N; i++) std::cout << a[i] << " "; + return 0; +} diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp new file mode 100644 index 000000000..794f65198 --- /dev/null +++ b/sorting/bubble_sort.cpp @@ -0,0 +1,83 @@ +/** + * @file + * @brief Bubble sort algorithm + * + * The working principle of the Bubble sort algorithm: + +Bubble sort algorithm is the bubble sorting algorithm. The most important reason +for calling the bubble is that the largest number is thrown at the end of this +algorithm. This is all about the logic. In each iteration, the largest number is +expired and when iterations are completed, the sorting takes place. + +What is Swap? + +Swap in the software means that two variables are displaced. +An additional variable is required for this operation. x = 5, y = 10. +We want x = 10, y = 5. Here we create the most variable to do it. + +int z; +z = x; +x = y; +y = z; + +The above process is a typical displacement process. +When x assigns the value to x, the old value of x is lost. +That's why we created a variable z to create the first value of the value of x, +and finally, we have assigned to y. + +Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) + +Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you +remember Big O Notation, we were calculating the complexity of the algorithms in +the nested loops. The n * (n - 1) product gives us O (n²) performance. In the +worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) +Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) +performance is taken. Bubble Sort Best Case Performance. O (n). However, you +can't get the best status in the code we shared above. This happens on the +optimized bubble sort algorithm. It's right down there. +*/ + +#include +#include + +int main() { + int n; + short swap_check = 1; + std::cout << "Enter the amount of numbers to sort: "; + std::cin >> n; + std::vector numbers; + std::cout << "Enter " << n << " numbers: "; + int num; + + // Input + for (int i = 0; i < n; i++) { + std::cin >> num; + numbers.push_back(num); + } + + // Bubble Sorting + for (int i = 0; (i < n) && (swap_check == 1); i++) { + swap_check = 0; + for (int j = 0; j < n - 1 - i; j++) { + if (numbers[j] > numbers[j + 1]) { + swap_check = 1; + std::swap(numbers[j], + numbers[j + 1]); // by changing swap location. + // I mean, j. If the number is + // greater than j + 1, then it + // means the location. + } + } + } + + // Output + std::cout << "\nSorted Array : "; + for (int i = 0; i < numbers.size(); i++) { + if (i != numbers.size() - 1) { + std::cout << numbers[i] << ", "; + } else { + std::cout << numbers[i] << std::endl; + } + } + return 0; +} diff --git a/sorting/bucketSort.cpp b/sorting/bucket_sort.cpp similarity index 100% rename from sorting/bucketSort.cpp rename to sorting/bucket_sort.cpp diff --git a/sorting/cocktail_selection_sort.cpp b/sorting/cocktail_selection_sort.cpp new file mode 100644 index 000000000..a9abd86d5 --- /dev/null +++ b/sorting/cocktail_selection_sort.cpp @@ -0,0 +1,101 @@ +// Returns Sorted elements after performing Cocktail Selection Sort +// It is a Sorting algorithm which chooses the minimum and maximum element in an +// array simultaneously, and swaps it with the lowest and highest available +// position iteratively or recursively + +#include +#include +#include + +// Iterative Version + +void CocktailSelectionSort(std::vector &vec, int low, int high) { + while (low <= high) { + int minimum = vec[low]; + int minimumindex = low; + int maximum = vec[high]; + int maximumindex = high; + + for (int i = low; i <= high; i++) { + if (vec[i] >= maximum) { + maximum = vec[i]; + maximumindex = i; + } + if (vec[i] <= minimum) { + minimum = vec[i]; + minimumindex = i; + } + } + if (low != maximumindex || high != minimumindex) { + std::swap(vec[low], vec[minimumindex]); + std::swap(vec[high], vec[maximumindex]); + } else { + std::swap(vec[low], vec[high]); + } + + low++; + high--; + } +} + +// Recursive Version + +void CocktailSelectionSort_v2(std::vector &vec, int low, int high) { + if (low >= high) return; + + int minimum = vec[low]; + int minimumindex = low; + int maximum = vec[high]; + int maximumindex = high; + + for (int i = low; i <= high; i++) { + if (vec[i] >= maximum) { + maximum = vec[i]; + maximumindex = i; + } + if (vec[i] <= minimum) { + minimum = vec[i]; + minimumindex = i; + } + } + if (low != maximumindex || high != minimumindex) { + std::swap(vec[low], vec[minimumindex]); + std::swap(vec[high], vec[maximumindex]); + } else { + std::swap(vec[low], vec[high]); + } + + CocktailSelectionSort(vec, low + 1, high - 1); +} + +// main function, select any one of iterative or recursive version + +int main() { + int n; + std::cout << "Enter number of elements\n"; + std::cin >> n; + std::vector v(n); + std::cout << "Enter all the elements\n"; + for (int i = 0; i < n; ++i) { + std::cin >> v[i]; + } + + int method; + std::cout << "Enter method: \n\t0: iterative\n\t1: recursive:\t"; + std::cin >> method; + + if (method == 0) { + CocktailSelectionSort(v, 0, n - 1); + } else if (method == 1) { + CocktailSelectionSort_v2(v, 0, n - 1); + } else { + std::cerr << "Unknown method" << std::endl; + return -1; + } + std::cout << "Sorted elements are\n"; + for (int i = 0; i < n; ++i) { + std::cout << v[i] << " "; + } + + return 0; +} diff --git a/sorting/combsort.cpp b/sorting/comb_sort.cpp similarity index 100% rename from sorting/combsort.cpp rename to sorting/comb_sort.cpp diff --git a/sorting/CountingSortString.cpp b/sorting/counting_sort_string.cpp similarity index 100% rename from sorting/CountingSortString.cpp rename to sorting/counting_sort_string.cpp diff --git a/sorting/doxy.txt b/sorting/doxy.txt deleted file mode 100644 index 68079276e..000000000 --- a/sorting/doxy.txt +++ /dev/null @@ -1,374 +0,0 @@ -# Doxyfile 1.8.13 -#This configuration file has been generated from Doxygen template. -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = "My Project" -PROJECT_NUMBER = -PROJECT_BRIEF = -PROJECT_LOGO = -OUTPUT_DIRECTORY = -CREATE_SUBDIRS = NO -ALLOW_UNICODE_NAMES = NO -OUTPUT_LANGUAGE = English -BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = YES -ABBREVIATE_BRIEF = "The $name class" \ - "The $name widget" \ - "The $name file" \ - is \ - provides \ - specifies \ - contains \ - represents \ - a \ - an \ - the -ALWAYS_DETAILED_SEC = NO -INLINE_INHERITED_MEMB = NO -FULL_PATH_NAMES = YES -STRIP_FROM_PATH = -STRIP_FROM_INC_PATH = -SHORT_NAMES = NO -JAVADOC_AUTOBRIEF = NO -QT_AUTOBRIEF = NO -MULTILINE_CPP_IS_BRIEF = NO -INHERIT_DOCS = YES -SEPARATE_MEMBER_PAGES = NO -TAB_SIZE = 4 -ALIASES = -TCL_SUBST = -OPTIMIZE_OUTPUT_FOR_C = NO -OPTIMIZE_OUTPUT_JAVA = NO -OPTIMIZE_FOR_FORTRAN = NO -OPTIMIZE_OUTPUT_VHDL = NO -EXTENSION_MAPPING = -MARKDOWN_SUPPORT = YES -TOC_INCLUDE_HEADINGS = 0 -AUTOLINK_SUPPORT = YES -BUILTIN_STL_SUPPORT = NO -CPP_CLI_SUPPORT = NO -SIP_SUPPORT = NO -IDL_PROPERTY_SUPPORT = YES -DISTRIBUTE_GROUP_DOC = NO -GROUP_NESTED_COMPOUNDS = NO -SUBGROUPING = YES -INLINE_GROUPED_CLASSES = NO -INLINE_SIMPLE_STRUCTS = NO -TYPEDEF_HIDES_STRUCT = NO -LOOKUP_CACHE_SIZE = 0 -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- -EXTRACT_ALL = NO -EXTRACT_PRIVATE = NO -EXTRACT_PACKAGE = NO -EXTRACT_STATIC = NO -EXTRACT_LOCAL_CLASSES = YES -EXTRACT_LOCAL_METHODS = NO -EXTRACT_ANON_NSPACES = NO -HIDE_UNDOC_MEMBERS = NO -HIDE_UNDOC_CLASSES = NO -HIDE_FRIEND_COMPOUNDS = NO -HIDE_IN_BODY_DOCS = NO -INTERNAL_DOCS = NO -CASE_SENSE_NAMES = YES -HIDE_SCOPE_NAMES = NO -HIDE_COMPOUND_REFERENCE= NO -SHOW_INCLUDE_FILES = YES -SHOW_GROUPED_MEMB_INC = NO -FORCE_LOCAL_INCLUDES = NO -INLINE_INFO = YES -SORT_MEMBER_DOCS = YES -SORT_BRIEF_DOCS = NO -SORT_MEMBERS_CTORS_1ST = NO -SORT_GROUP_NAMES = NO -SORT_BY_SCOPE_NAME = NO -STRICT_PROTO_MATCHING = NO -GENERATE_TODOLIST = YES -GENERATE_TESTLIST = YES -GENERATE_BUGLIST = YES -GENERATE_DEPRECATEDLIST= YES -ENABLED_SECTIONS = -MAX_INITIALIZER_LINES = 30 -SHOW_USED_FILES = YES -SHOW_FILES = YES -SHOW_NAMESPACES = YES -FILE_VERSION_FILTER = -LAYOUT_FILE = -CITE_BIB_FILES = -#--------------------------------------------------------------------------- -# Configuration options related to warning and progress messages -#--------------------------------------------------------------------------- -QUIET = NO -WARNINGS = YES -WARN_IF_UNDOCUMENTED = YES -WARN_IF_DOC_ERROR = YES -WARN_NO_PARAMDOC = NO -WARN_AS_ERROR = NO -WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = -#--------------------------------------------------------------------------- -# Configuration options related to the input files -#--------------------------------------------------------------------------- -INPUT = -INPUT_ENCODING = UTF-8 -FILE_PATTERNS = *.c \ - *.cc \ - *.cxx \ - *.cpp \ - *.c++ \ - *.java \ - *.ii \ - *.ixx \ - *.ipp \ - *.i++ \ - *.inl \ - *.idl \ - *.ddl \ - *.odl \ - *.h \ - *.hh \ - *.hxx \ - *.hpp \ - *.h++ \ - *.cs \ - *.d \ - *.php \ - *.php4 \ - *.php5 \ - *.phtml \ - *.inc \ - *.m \ - *.markdown \ - *.md \ - *.mm \ - *.dox \ - *.py \ - *.pyw \ - *.f90 \ - *.f95 \ - *.f03 \ - *.f08 \ - *.f \ - *.for \ - *.tcl \ - *.vhd \ - *.vhdl \ - *.ucf \ - *.qsf -RECURSIVE = NO -EXCLUDE = -EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = -EXCLUDE_SYMBOLS = -EXAMPLE_PATH = -EXAMPLE_PATTERNS = * -EXAMPLE_RECURSIVE = NO -IMAGE_PATH = -INPUT_FILTER = -FILTER_PATTERNS = -FILTER_SOURCE_FILES = NO -FILTER_SOURCE_PATTERNS = -USE_MDFILE_AS_MAINPAGE = -#--------------------------------------------------------------------------- -# Configuration options related to source browsing -#--------------------------------------------------------------------------- -SOURCE_BROWSER = NO -INLINE_SOURCES = NO -STRIP_CODE_COMMENTS = YES -REFERENCED_BY_RELATION = NO -REFERENCES_RELATION = NO -REFERENCES_LINK_SOURCE = YES -SOURCE_TOOLTIPS = YES -USE_HTAGS = NO -VERBATIM_HEADERS = YES -CLANG_ASSISTED_PARSING = NO -CLANG_OPTIONS = -#--------------------------------------------------------------------------- -# Configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- -ALPHABETICAL_INDEX = YES -COLS_IN_ALPHA_INDEX = 5 -IGNORE_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the HTML output -#--------------------------------------------------------------------------- -GENERATE_HTML = YES -HTML_OUTPUT = html -HTML_FILE_EXTENSION = .html -HTML_HEADER = -HTML_FOOTER = -HTML_STYLESHEET = -HTML_EXTRA_STYLESHEET = -HTML_EXTRA_FILES = -HTML_COLORSTYLE_HUE = 220 -HTML_COLORSTYLE_SAT = 100 -HTML_COLORSTYLE_GAMMA = 80 -HTML_TIMESTAMP = NO -HTML_DYNAMIC_SECTIONS = NO -HTML_INDEX_NUM_ENTRIES = 100 -GENERATE_DOCSET = NO -DOCSET_FEEDNAME = "Doxygen generated docs" -DOCSET_BUNDLE_ID = org.doxygen.Project -DOCSET_PUBLISHER_ID = org.doxygen.Publisher -DOCSET_PUBLISHER_NAME = Publisher -GENERATE_HTMLHELP = NO -CHM_FILE = -HHC_LOCATION = -GENERATE_CHI = NO -CHM_INDEX_ENCODING = -BINARY_TOC = NO -TOC_EXPAND = NO -GENERATE_QHP = NO -QCH_FILE = -QHP_NAMESPACE = org.doxygen.Project -QHP_VIRTUAL_FOLDER = doc -QHP_CUST_FILTER_NAME = -QHP_CUST_FILTER_ATTRS = -QHP_SECT_FILTER_ATTRS = -QHG_LOCATION = -GENERATE_ECLIPSEHELP = NO -ECLIPSE_DOC_ID = org.doxygen.Project -DISABLE_INDEX = NO -GENERATE_TREEVIEW = NO -ENUM_VALUES_PER_LINE = 4 -TREEVIEW_WIDTH = 250 -EXT_LINKS_IN_WINDOW = NO -FORMULA_FONTSIZE = 10 -FORMULA_TRANSPARENT = YES -USE_MATHJAX = NO -MATHJAX_FORMAT = HTML-CSS -MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest -MATHJAX_EXTENSIONS = -MATHJAX_CODEFILE = -SEARCHENGINE = YES -SERVER_BASED_SEARCH = NO -EXTERNAL_SEARCH = NO -SEARCHENGINE_URL = -SEARCHDATA_FILE = searchdata.xml -EXTERNAL_SEARCH_ID = -EXTRA_SEARCH_MAPPINGS = -#--------------------------------------------------------------------------- -# Configuration options related to the LaTeX output -#--------------------------------------------------------------------------- -GENERATE_LATEX = NO -LATEX_OUTPUT = latex -LATEX_CMD_NAME = latex -MAKEINDEX_CMD_NAME = makeindex -COMPACT_LATEX = NO -PAPER_TYPE = a4 -EXTRA_PACKAGES = -LATEX_HEADER = -LATEX_FOOTER = -LATEX_EXTRA_STYLESHEET = -LATEX_EXTRA_FILES = -PDF_HYPERLINKS = YES -USE_PDFLATEX = YES -LATEX_BATCHMODE = NO -LATEX_HIDE_INDICES = NO -LATEX_SOURCE_CODE = NO -LATEX_BIB_STYLE = plain -LATEX_TIMESTAMP = NO -#--------------------------------------------------------------------------- -# Configuration options related to the RTF output -#--------------------------------------------------------------------------- -GENERATE_RTF = NO -RTF_OUTPUT = rtf -COMPACT_RTF = NO -RTF_HYPERLINKS = NO -RTF_STYLESHEET_FILE = -RTF_EXTENSIONS_FILE = -RTF_SOURCE_CODE = NO -#--------------------------------------------------------------------------- -# Configuration options related to the man page output -#--------------------------------------------------------------------------- -GENERATE_MAN = NO -MAN_OUTPUT = man -MAN_EXTENSION = .3 -MAN_SUBDIR = -MAN_LINKS = NO -#--------------------------------------------------------------------------- -# Configuration options related to the XML output -#--------------------------------------------------------------------------- -GENERATE_XML = NO -XML_OUTPUT = xml -XML_PROGRAMLISTING = YES -#--------------------------------------------------------------------------- -# Configuration options related to the DOCBOOK output -#--------------------------------------------------------------------------- -GENERATE_DOCBOOK = NO -DOCBOOK_OUTPUT = docbook -DOCBOOK_PROGRAMLISTING = NO -#--------------------------------------------------------------------------- -# Configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- -GENERATE_AUTOGEN_DEF = NO -#--------------------------------------------------------------------------- -# Configuration options related to the Perl module output -#--------------------------------------------------------------------------- -GENERATE_PERLMOD = NO -PERLMOD_LATEX = NO -PERLMOD_PRETTY = YES -PERLMOD_MAKEVAR_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- -ENABLE_PREPROCESSING = YES -MACRO_EXPANSION = NO -EXPAND_ONLY_PREDEF = NO -SEARCH_INCLUDES = YES -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = -PREDEFINED = -EXPAND_AS_DEFINED = -SKIP_FUNCTION_MACROS = YES -#--------------------------------------------------------------------------- -# Configuration options related to external references -#--------------------------------------------------------------------------- -TAGFILES = -GENERATE_TAGFILE = -ALLEXTERNALS = NO -EXTERNAL_GROUPS = YES -EXTERNAL_PAGES = YES -PERL_PATH = /usr/bin/perl -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- -CLASS_DIAGRAMS = YES -MSCGEN_PATH = -DIA_PATH = -HIDE_UNDOC_RELATIONS = YES -HAVE_DOT = YES -DOT_NUM_THREADS = 0 -DOT_FONTNAME = Helvetica -DOT_FONTSIZE = 10 -DOT_FONTPATH = -CLASS_GRAPH = YES -COLLABORATION_GRAPH = YES -GROUP_GRAPHS = YES -UML_LOOK = NO -UML_LIMIT_NUM_FIELDS = 10 -TEMPLATE_RELATIONS = NO -INCLUDE_GRAPH = YES -INCLUDED_BY_GRAPH = YES -CALL_GRAPH = NO -CALLER_GRAPH = NO -GRAPHICAL_HIERARCHY = YES -DIRECTORY_GRAPH = YES -DOT_IMAGE_FORMAT = png -INTERACTIVE_SVG = NO -DOT_PATH = -DOTFILE_DIRS = -MSCFILE_DIRS = -DIAFILE_DIRS = -PLANTUML_JAR_PATH = -PLANTUML_CFG_FILE = -PLANTUML_INCLUDE_PATH = -DOT_GRAPH_MAX_NODES = 50 -MAX_DOT_GRAPH_DEPTH = 0 -DOT_TRANSPARENT = NO -DOT_MULTI_TARGETS = NO -GENERATE_LEGEND = YES -DOT_CLEANUP = YES diff --git a/sorting/Insertion Sort.cpp b/sorting/insertion_sort.cpp similarity index 100% rename from sorting/Insertion Sort.cpp rename to sorting/insertion_sort.cpp diff --git a/sorting/makefile b/sorting/makefile deleted file mode 100644 index 21d3186c6..000000000 --- a/sorting/makefile +++ /dev/null @@ -1,11 +0,0 @@ -non_recursive_merge_sort: non_recursive_merge_sort.cpp - g++ -std=c++17 -o non_recursive_merge_sort non_recursive_merge_sort.cpp -.PHONY: test -.PHONY: doc -.PHONY: clean -test: non_recursive_merge_sort - ./non_recursive_merge_sort -doc: doxy.txt - doxygen doxy.txt -clean: - rm -fr non_recursive_merge_sort html diff --git a/sorting/Merge Sort.cpp b/sorting/merge_sort.cpp similarity index 100% rename from sorting/Merge Sort.cpp rename to sorting/merge_sort.cpp diff --git a/sorting/non_recursive_merge_sort.cpp b/sorting/non_recursive_merge_sort.cpp index c6ca56376..ebfef1dcc 100644 --- a/sorting/non_recursive_merge_sort.cpp +++ b/sorting/non_recursive_merge_sort.cpp @@ -6,7 +6,7 @@ */ #include // for size_t #include // for std::move & std::remove_reference_t -template +template void merge(Iterator, Iterator, const Iterator, char[]); /// bottom-up merge sort which sorts elements in a non-decreasing order /** @@ -17,13 +17,13 @@ void merge(Iterator, Iterator, const Iterator, char[]); * @param first points to the first element * @param last points to 1-step past the last element * @param n the number of elements -*/ -template + */ +template void non_recursive_merge_sort(const Iterator first, const Iterator last, const size_t n) { // create a buffer large enough to store all elements // dynamically allocated to comply with cpplint - char * buffer = new char[n * sizeof(*first)]; + char* buffer = new char[n * sizeof(*first)]; // buffer size can be optimized to largest power of 2 less than n elements // divide the container into equally-sized segments whose length start at 1 // and keeps increasing by factors of 2 @@ -49,32 +49,28 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last, * @param r points to the right part, end of left part * @param e points to end of right part * @param b points at the buffer -*/ -template + */ +template void merge(Iterator l, Iterator r, const Iterator e, char b[]) { // create 2 pointers to point at the buffer auto p(reinterpret_cast*>(b)), c(p); // move the left part of the segment - for (Iterator t(l); r != t; ++t) - *p++ = std::move(*t); + for (Iterator t(l); r != t; ++t) *p++ = std::move(*t); // while neither the buffer nor the right part has been exhausted // move the smallest element of the two back to the container - while (e != r && c != p) - *l++ = std::move(*r < *c ? *r++ : *c++); + while (e != r && c != p) *l++ = std::move(*r < *c ? *r++ : *c++); // notice only one of the two following loops will be executed // while the right part hasn't bee exhausted, move it back - while (e != r) - *l++ = std::move(*r++); + while (e != r) *l++ = std::move(*r++); // while the buffer hasn't bee exhausted, move it back - while (c != p) - *l++ = std::move(*c++); + while (c != p) *l++ = std::move(*c++); } /// bottom-up merge sort which sorts elements in a non-decreasing order /** * @param first points to the first element * @param n the number of elements -*/ -template + */ +template void non_recursive_merge_sort(const Iterator first, const size_t n) { non_recursive_merge_sort(first, first + n, n); } @@ -82,8 +78,8 @@ void non_recursive_merge_sort(const Iterator first, const size_t n) { /** * @param first points to the first element * @param last points to 1-step past the last element -*/ -template + */ +template void non_recursive_merge_sort(const Iterator first, const Iterator last) { non_recursive_merge_sort(first, last, last - first); } @@ -92,15 +88,15 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last) { * Currently, it only creates output for non_recursive_merge_sort.cpp, but if * it has proven its efficacy it can be expanded to other files. * The configuration file is named doxy.txt and has been auto-generated too. -*/ + */ // the remaining of this file is only for testing. It can erased to to convert // it into a header file for later re-use. #include -int main(int argc, char ** argv) { +int main(int argc, char** argv) { int size; std::cout << "Enter the number of elements : "; std::cin >> size; - int * arr = new int[size]; + int* arr = new int[size]; for (int i = 0; i < size; ++i) { std::cout << "arr[" << i << "] = "; std::cin >> arr[i]; diff --git a/sorting/numeric_string_sort.cpp b/sorting/numeric_string_sort.cpp new file mode 100644 index 000000000..8b86bb29d --- /dev/null +++ b/sorting/numeric_string_sort.cpp @@ -0,0 +1,54 @@ +// Using general algorithms to sort a collection of strings results in +// alphanumeric sort. If it is a numeric string, it leads to unnatural sorting + +// eg, an array of strings 1,10,100,2,20,200,3,30,300 +// would be sorted in that same order by using conventional sorting, +// even though we know the correct sorting order is 1,2,3,10,20,30,100,200,300 + +// This Programme uses a comparator to sort the array in Numerical order instead +// of Alphanumeric order + +#include +#include +#include +#include + +bool NumericSort(std::string a, std::string b) { + while (a[0] == '0') { + a.erase(a.begin()); + } + while (b[0] == '0') { + b.erase(b.begin()); + } + int n = a.length(); + int m = b.length(); + if (n == m) return a < b; + return n < m; +} + +int main() { + int n; + std::cout << "Enter number of elements to be sorted Numerically\n"; + std::cin >> n; + + std::vector v(n); + std::cout << "Enter the string of Numbers\n"; + for (int i = 0; i < n; i++) { + std::cin >> v[i]; + } + + sort(v.begin(), v.end()); + std::cout << "Elements sorted normally \n"; + for (int i = 0; i < n; i++) { + std::cout << v[i] << " "; + } + std::cout << "\n"; + + std::sort(v.begin(), v.end(), NumericSort); + std::cout << "Elements sorted Numerically \n"; + for (int i = 0; i < n; i++) { + std::cout << v[i] << " "; + } + + return 0; +} diff --git a/sorting/OddEven Sort.cpp b/sorting/odd_even_sort.cpp similarity index 100% rename from sorting/OddEven Sort.cpp rename to sorting/odd_even_sort.cpp diff --git a/sorting/Quick Sort.cpp b/sorting/quick_sort.cpp similarity index 100% rename from sorting/Quick Sort.cpp rename to sorting/quick_sort.cpp diff --git a/sorting/Radix Sort.cpp b/sorting/radix_sort.cpp similarity index 100% rename from sorting/Radix Sort.cpp rename to sorting/radix_sort.cpp diff --git a/sorting/Selection Sort.cpp b/sorting/selection_sort.cpp similarity index 100% rename from sorting/Selection Sort.cpp rename to sorting/selection_sort.cpp diff --git a/sorting/Shell Sort.cpp b/sorting/shell_sort.cpp similarity index 100% rename from sorting/Shell Sort.cpp rename to sorting/shell_sort.cpp diff --git a/sorting/Slow Sort.cpp b/sorting/slow_sort.cpp similarity index 100% rename from sorting/Slow Sort.cpp rename to sorting/slow_sort.cpp diff --git a/sorting/Tim Sort.cpp b/sorting/tim_sort.cpp similarity index 100% rename from sorting/Tim Sort.cpp rename to sorting/tim_sort.cpp From 887bff8a9ead3c19b03b73f91384e92de98d05cf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 11:46:24 -0400 Subject: [PATCH 13/28] sorting fixes for MSVC and CPPLINT (cherry picked from commit 76a5f572d5d9834585201836183027e3575b1e55) --- sorting/bead_sort.cpp | 5 ++-- sorting/bucket_sort.cpp | 51 ++++++++++++++++++----------------------- sorting/comb_sort.cpp | 47 +++++++++++++++---------------------- 3 files changed, 43 insertions(+), 60 deletions(-) diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp index 12b7d4d32..d34706073 100644 --- a/sorting/bead_sort.cpp +++ b/sorting/bead_sort.cpp @@ -12,8 +12,8 @@ void beadSort(int *a, int len) { if (a[i] > max) max = a[i]; // allocating memory - unsigned char beads[max * len]; - memset(beads, 0, sizeof(beads)); + unsigned char *beads = new unsigned char[max * len]; + memset(beads, 0, max * len); // mark the beads for (int i = 0; i < len; i++) @@ -39,6 +39,7 @@ void beadSort(int *a, int len) { a[i] = j; } + delete[] beads; } // driver function to test the algorithm diff --git a/sorting/bucket_sort.cpp b/sorting/bucket_sort.cpp index 0ffbf8e4c..f120f04fc 100644 --- a/sorting/bucket_sort.cpp +++ b/sorting/bucket_sort.cpp @@ -1,42 +1,35 @@ // C++ program to sort an array using bucket sort -#include #include +#include #include -using namespace std; // Function to sort arr[] of size n using bucket sort -void bucketSort(float arr[], int n) -{ - // 1) Create n empty buckets - vector b[n]; +void bucketSort(float arr[], int n) { + // 1) Create n empty buckets + std::vector b[n]; - // 2) Put array elements in different buckets - for (int i = 0; i < n; i++) - { - int bi = n * arr[i]; // Index in bucket - b[bi].push_back(arr[i]); - } + // 2) Put array elements in different buckets + for (int i = 0; i < n; i++) { + int bi = n * arr[i]; // Index in bucket + b[bi].push_back(arr[i]); + } - // 3) Sort individual buckets - for (int i = 0; i < n; i++) - sort(b[i].begin(), b[i].end()); + // 3) Sort individual buckets + for (int i = 0; i < n; i++) std::sort(b[i].begin(), b[i].end()); - // 4) Concatenate all buckets into arr[] - int index = 0; - for (int i = 0; i < n; i++) - for (int j = 0; j < b[i].size(); j++) - arr[index++] = b[i][j]; + // 4) Concatenate all buckets into arr[] + int index = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < b[i].size(); j++) arr[index++] = b[i][j]; } /* Driver program to test above funtion */ -int main() -{ - float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; - int n = sizeof(arr) / sizeof(arr[0]); - bucketSort(arr, n); +int main() { + float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; + int n = sizeof(arr) / sizeof(arr[0]); + bucketSort(arr, n); - cout << "Sorted array is \n"; - for (int i = 0; i < n; i++) - cout << arr[i] << " "; - return 0; + std::cout << "Sorted array is \n"; + for (int i = 0; i < n; i++) std::cout << arr[i] << " "; + return 0; } diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 2209c96fc..feed4ba44 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -1,59 +1,48 @@ -//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) +// 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) #include -using namespace std; - int a[100005]; int n; -int FindNextGap(int x) -{ +int FindNextGap(int x) { x = (x * 10) / 13; - return max(1, x); + return std::max(1, x); } -void CombSort(int a[], int l, int r) -{ - //Init gap +void CombSort(int a[], int l, int r) { + // Init gap int gap = n; - //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 - while (gap != 1 || swapped) - { - //Find next gap + // Keep running until gap = 1 or none elements were swapped + while (gap != 1 || swapped) { + // Find next gap gap = FindNextGap(gap); swapped = false; // Compare all elements with current gap - for (int i = l; i <= r - gap; ++i) - { - if (a[i] > a[i + gap]) - { - swap(a[i], a[i + gap]); + for (int i = l; i <= r - gap; ++i) { + if (a[i] > a[i + gap]) { + std::swap(a[i], a[i + gap]); swapped = true; } } } } -int main() -{ - cin >> n; - for (int i = 1; i <= n; ++i) - cin >> a[i]; +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) - cout << a[i] << ' '; + for (int i = 1; i <= n; ++i) std::cout << a[i] << ' '; return 0; } From 77e781f709308a393b8e0ab11ac2f6b5dec15a68 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 12:15:12 -0400 Subject: [PATCH 14/28] cpplint and msvc fixes for - sorting (cherry picked from commit 0d8e015d15b87ea188b75f1f70b7be614cdf86cd) --- sorting/bead_sort.cpp | 4 +-- sorting/bubble_sort.cpp | 8 +++--- sorting/cocktail_selection_sort.cpp | 44 ++++++++++++++--------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp index d34706073..a1a54c1e0 100644 --- a/sorting/bead_sort.cpp +++ b/sorting/bead_sort.cpp @@ -34,8 +34,8 @@ void beadSort(int *a, int len) { // Put sorted values in array using beads for (int i = 0; i < len; i++) { int j; - for (j = 0; j < max && BEAD(i, j); j++) - ; + for (j = 0; j < max && BEAD(i, j); j++) { + } a[i] = j; } diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 794f65198..c43e425fc 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -42,7 +42,7 @@ optimized bubble sort algorithm. It's right down there. int main() { int n; - short swap_check = 1; + bool swap_check = true; std::cout << "Enter the amount of numbers to sort: "; std::cin >> n; std::vector numbers; @@ -56,11 +56,11 @@ int main() { } // Bubble Sorting - for (int i = 0; (i < n) && (swap_check == 1); i++) { - swap_check = 0; + for (int i = 0; (i < n) && (swap_check); i++) { + swap_check = false; for (int j = 0; j < n - 1 - i; j++) { if (numbers[j] > numbers[j + 1]) { - swap_check = 1; + swap_check = true; std::swap(numbers[j], numbers[j + 1]); // by changing swap location. // I mean, j. If the number is diff --git a/sorting/cocktail_selection_sort.cpp b/sorting/cocktail_selection_sort.cpp index a9abd86d5..ab6229996 100644 --- a/sorting/cocktail_selection_sort.cpp +++ b/sorting/cocktail_selection_sort.cpp @@ -9,28 +9,28 @@ // Iterative Version -void CocktailSelectionSort(std::vector &vec, int low, int high) { +void CocktailSelectionSort(std::vector *vec, int low, int high) { while (low <= high) { - int minimum = vec[low]; + int minimum = (*vec)[low]; int minimumindex = low; - int maximum = vec[high]; + int maximum = (*vec)[high]; int maximumindex = high; for (int i = low; i <= high; i++) { - if (vec[i] >= maximum) { - maximum = vec[i]; + if ((*vec)[i] >= maximum) { + maximum = (*vec)[i]; maximumindex = i; } - if (vec[i] <= minimum) { - minimum = vec[i]; + if ((*vec)[i] <= minimum) { + minimum = (*vec)[i]; minimumindex = i; } } if (low != maximumindex || high != minimumindex) { - std::swap(vec[low], vec[minimumindex]); - std::swap(vec[high], vec[maximumindex]); + std::swap((*vec)[low], (*vec)[minimumindex]); + std::swap((*vec)[high], (*vec)[maximumindex]); } else { - std::swap(vec[low], vec[high]); + std::swap((*vec)[low], (*vec)[high]); } low++; @@ -40,29 +40,29 @@ void CocktailSelectionSort(std::vector &vec, int low, int high) { // Recursive Version -void CocktailSelectionSort_v2(std::vector &vec, int low, int high) { +void CocktailSelectionSort_v2(std::vector *vec, int low, int high) { if (low >= high) return; - int minimum = vec[low]; + int minimum = (*vec)[low]; int minimumindex = low; - int maximum = vec[high]; + int maximum = (*vec)[high]; int maximumindex = high; for (int i = low; i <= high; i++) { - if (vec[i] >= maximum) { - maximum = vec[i]; + if ((*vec)[i] >= maximum) { + maximum = (*vec)[i]; maximumindex = i; } - if (vec[i] <= minimum) { - minimum = vec[i]; + if ((*vec)[i] <= minimum) { + minimum = (*vec)[i]; minimumindex = i; } } if (low != maximumindex || high != minimumindex) { - std::swap(vec[low], vec[minimumindex]); - std::swap(vec[high], vec[maximumindex]); + std::swap((*vec)[low], (*vec)[minimumindex]); + std::swap((*vec)[high], (*vec)[maximumindex]); } else { - std::swap(vec[low], vec[high]); + std::swap((*vec)[low], (*vec)[high]); } CocktailSelectionSort(vec, low + 1, high - 1); @@ -85,9 +85,9 @@ int main() { std::cin >> method; if (method == 0) { - CocktailSelectionSort(v, 0, n - 1); + CocktailSelectionSort(&v, 0, n - 1); } else if (method == 1) { - CocktailSelectionSort_v2(v, 0, n - 1); + CocktailSelectionSort_v2(&v, 0, n - 1); } else { std::cerr << "Unknown method" << std::endl; return -1; From 09f073306527f5f5d47f1f0c046d526902f080d0 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 12:31:49 -0400 Subject: [PATCH 15/28] fix dynamic array (cherry picked from commit 717f5c8f01c1d5559320e118ccc8eabdf602e8ff) --- sorting/bucket_sort.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorting/bucket_sort.cpp b/sorting/bucket_sort.cpp index f120f04fc..c43865281 100644 --- a/sorting/bucket_sort.cpp +++ b/sorting/bucket_sort.cpp @@ -6,7 +6,7 @@ // Function to sort arr[] of size n using bucket sort void bucketSort(float arr[], int n) { // 1) Create n empty buckets - std::vector b[n]; + std::vector *b = new std::vector[n]; // 2) Put array elements in different buckets for (int i = 0; i < n; i++) { @@ -21,6 +21,7 @@ void bucketSort(float arr[], int n) { int index = 0; for (int i = 0; i < n; i++) for (int j = 0; j < b[i].size(); j++) arr[index++] = b[i][j]; + delete[] b; } /* Driver program to test above funtion */ From 231c99f8802346b84962bb087f37ed34d1a0d3fc Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 13:02:10 -0400 Subject: [PATCH 16/28] fix dynamic array issues in sorting folder (cherry picked from commit 01b69fcb249c30d4f1795766bf4cc617d8200ab3) --- sorting/comb_sort.cpp | 1 + sorting/insertion_sort.cpp | 62 ++++++++++---------- sorting/merge_sort.cpp | 66 +++++++++------------ sorting/quick_sort.cpp | 51 +++++++--------- sorting/radix_sort.cpp | 116 +++++++++++++++++-------------------- sorting/shell_sort.cpp | 72 ++++++++++------------- sorting/slow_sort.cpp | 82 +++++++++++++------------- sorting/swap_sort.cpp | 7 ++- sorting/tim_sort.cpp | 110 ++++++++++++++++------------------- 9 files changed, 256 insertions(+), 311 deletions(-) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index feed4ba44..1b0a4d706 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -2,6 +2,7 @@ // While Bubble sort is comparering adjacent value, Combsort is using gap larger // than 1 Best case: O(n) Worst case: O(n ^ 2) +#include #include int a[100005]; diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 2563acaf8..fe920ca59 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -1,40 +1,36 @@ -//Insertion Sort +// Insertion Sort #include -using namespace std; -int main() -{ - int n; - cout << "\nEnter the length of your array : "; - cin >> n; - int Array[n]; - cout << "\nEnter any " << n << " Numbers for Unsorted Array : "; +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++) - { - cin >> Array[i]; - } + // Input + for (int i = 0; i < n; i++) { + std::cin >> Array[i]; + } - //Sorting - for (int i = 1; i < n; i++) - { - int temp = Array[i]; - int j = i - 1; - while (j >= 0 && temp < Array[j]) - { - Array[j + 1] = Array[j]; - j--; - } - Array[j + 1] = temp; - } + // Sorting + for (int i = 1; i < n; i++) { + int temp = Array[i]; + int j = i - 1; + while (j >= 0 && temp < Array[j]) { + Array[j + 1] = Array[j]; + j--; + } + Array[j + 1] = temp; + } - //Output - cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) - { - cout << Array[i] << "\t"; - } - return 0; + // Output + std::cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) { + std::cout << Array[i] << "\t"; + } + + delete[] Array; + return 0; } diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index b8edc8851..82ab869cd 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -1,57 +1,47 @@ #include -using namespace std; -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; - int L[n1], R[n2]; + int *L = new int[n1], *R = new int[n2]; - for (i = 0; i < n1; i++) - L[i] = arr[l + i]; - for (j = 0; j < n2; j++) - R[j] = arr[m + 1 + j]; + for (i = 0; i < n1; i++) L[i] = arr[l + i]; + for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; - while (i < n1 && j < n2) - { - if (L[i] <= R[j]) - { + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { arr[k] = L[i]; i++; - } - else - { + } else { arr[k] = R[j]; j++; } k++; } - while (i < n1) - { + while (i < n1) { arr[k] = L[i]; i++; k++; } - while (j < n2) - { + while (j < n2) { arr[k] = R[j]; j++; k++; } + + delete[] L; + delete[] R; } -void mergeSort(int arr[], int l, int r) -{ - if (l < r) - { - +void mergeSort(int arr[], int l, int r) { + if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); @@ -61,33 +51,31 @@ void mergeSort(int arr[], int l, int r) } } -void show(int A[], int size) -{ +void show(int A[], int size) { int i; - for (i = 0; i < size; i++) - cout << A[i] << "\n"; + for (i = 0; i < size; i++) std::cout << A[i] << "\n"; } -int main() -{ +int main() { int size; - cout << "\nEnter the number of elements : "; + std::cout << "\nEnter the number of elements : "; - cin >> size; + std::cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + std::cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; + for (int i = 0; i < size; ++i) { + std::cout << "\n"; + std::cin >> arr[i]; } mergeSort(arr, 0, size); - cout << "Sorted array\n"; + std::cout << "Sorted array\n"; show(arr, size); + + delete[] arr; return 0; } diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 0b807898f..489e10965 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -1,19 +1,15 @@ /* C implementation QuickSort */ #include -using namespace std; -int partition(int arr[], int low, int high) -{ - int pivot = arr[high]; // pivot - int i = (low - 1); // Index of smaller element +int partition(int arr[], int low, int high) { + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element - for (int j = low; j < high; j++) - { + for (int j = low; j < high; j++) { // If current element is smaller than or // equal to pivot - if (arr[j] <= pivot) - { - i++; // increment index of smaller element + if (arr[j] <= pivot) { + i++; // increment index of smaller element int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; @@ -25,11 +21,8 @@ int partition(int arr[], int low, int high) return (i + 1); } -void quickSort(int arr[], int low, int high) -{ - if (low < high) - { - +void quickSort(int arr[], int low, int high) { + if (low < high) { int p = partition(arr, low, high); quickSort(arr, low, p - 1); @@ -37,31 +30,29 @@ void quickSort(int arr[], int low, int high) } } -void show(int arr[], int size) -{ - for (int i = 0; i < size; i++) - cout << arr[i] << "\n"; +void show(int arr[], int size) { + for (int i = 0; i < size; i++) std::cout << arr[i] << "\n"; } // Driver program to test above functions -int main() -{ +int main() { int size; - cout << "\nEnter the number of elements : "; + std::cout << "\nEnter the number of elements : "; - cin >> size; + std::cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + std::cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; + for (int i = 0; i < size; ++i) { + std::cout << "\n"; + std::cin >> arr[i]; } quickSort(arr, 0, size); - cout << "Sorted array\n"; + std::cout << "Sorted array\n"; show(arr, size); + + delete[] arr; return 0; } diff --git a/sorting/radix_sort.cpp b/sorting/radix_sort.cpp index 09c91bb22..e5bc3db84 100644 --- a/sorting/radix_sort.cpp +++ b/sorting/radix_sort.cpp @@ -1,68 +1,58 @@ -#include -#include #include +#include #include -using namespace std; -void radixsort(int a[], int n) -{ - int count[10]; - int output[n]; - memset(output, 0, sizeof(output)); - memset(count, 0, sizeof(count)); - int max = 0; - for (int i = 0; i < n; ++i) - { - if (a[i] > max) - { - max = a[i]; - } - } - int maxdigits = 0; - while (max) - { - maxdigits++; - max /= 10; - } - for (int j = 0; j < maxdigits; j++) - { - for (int i = 0; i < n; i++) - { - int t = pow(10, j); - count[(a[i] % (10 * t)) / t]++; - } - int k = 0; - for (int p = 0; p < 10; p++) - { - for (int i = 0; i < n; i++) - { - int t = pow(10, j); - if ((a[i] % (10 * t)) / t == p) - { - output[k] = a[i]; - k++; - } - } - } - memset(count, 0, sizeof(count)); - for (int i = 0; i < n; ++i) - { - a[i] = output[i]; - } - } +#include + +void radixsort(int a[], int n) { + int count[10]; + int* output = new int[n]; + memset(output, 0, n * sizeof(*output)); + memset(count, 0, sizeof(count)); + int max = 0; + for (int i = 0; i < n; ++i) { + if (a[i] > max) { + max = a[i]; + } + } + int maxdigits = 0; + while (max) { + maxdigits++; + max /= 10; + } + for (int j = 0; j < maxdigits; j++) { + for (int i = 0; i < n; i++) { + int t = std::pow(10, j); + count[(a[i] % (10 * t)) / t]++; + } + int k = 0; + for (int p = 0; p < 10; p++) { + for (int i = 0; i < n; i++) { + int t = std::pow(10, j); + if ((a[i] % (10 * t)) / t == p) { + output[k] = a[i]; + k++; + } + } + } + memset(count, 0, sizeof(count)); + for (int i = 0; i < n; ++i) { + a[i] = output[i]; + } + } + delete[] output; } -void print(int a[], int n) -{ - for (int i = 0; i < n; ++i) - { - cout << a[i] << " "; - } - cout << endl; + +void print(int a[], int n) { + for (int i = 0; i < n; ++i) { + std::cout << a[i] << " "; + } + std::cout << std::endl; } -int main(int argc, char const *argv[]) -{ - int a[] = {170, 45, 75, 90, 802, 24, 2, 66}; - int n = sizeof(a) / sizeof(a[0]); - radixsort(a, n); - print(a, n); - return 0; + +int main(int argc, char const* argv[]) { + int a[] = {170, 45, 75, 90, 802, 24, 2, 66}; + int n = sizeof(a) / sizeof(a[0]); + radixsort(a, n); + print(a, n); + return 0; } \ No newline at end of file diff --git a/sorting/shell_sort.cpp b/sorting/shell_sort.cpp index b08c7ffd8..eb701478d 100644 --- a/sorting/shell_sort.cpp +++ b/sorting/shell_sort.cpp @@ -1,45 +1,37 @@ #include -using namespace std; -int main() -{ - int size = 10; - int array[size]; - // Input - cout << "\nHow many numbers do want to enter in unsorted array : "; - cin >> size; - cout << "\nEnter the numbers for unsorted array : "; - for (int i = 0; i < size; i++) - { - cin >> array[i]; - } +int main() { + int size = 10; + int* array = new int[size]; + // Input + std::cout << "\nHow many numbers do want to enter in unsorted array : "; + std::cin >> size; + std::cout << "\nEnter the numbers for unsorted array : "; + for (int i = 0; i < size; i++) { + std::cin >> array[i]; + } - // Sorting - for (int i = size / 2; i > 0; i = i / 2) - { - for (int j = i; j < size; j++) - { - for (int k = j - i; k >= 0; k = k - i) - { - if (array[k] < array[k + i]) - { - break; - } - else - { - int temp = array[k + i]; - array[k + i] = array[k]; - array[k] = temp; - } - } - } - } + // Sorting + for (int i = size / 2; i > 0; i = i / 2) { + for (int j = i; j < size; j++) { + for (int k = j - i; k >= 0; k = k - i) { + if (array[k] < array[k + i]) { + break; + } else { + int temp = array[k + i]; + array[k + i] = array[k]; + array[k] = temp; + } + } + } + } - // Output - cout << "\nSorted array : "; - for (int i = 0; i < size; ++i) - { - cout << array[i] << "\t"; - } - return 0; + // Output + std::cout << "\nSorted array : "; + for (int i = 0; i < size; ++i) { + std::cout << array[i] << "\t"; + } + + delete[] array; + return 0; } diff --git a/sorting/slow_sort.cpp b/sorting/slow_sort.cpp index f8f7e74b8..2872b37d8 100644 --- a/sorting/slow_sort.cpp +++ b/sorting/slow_sort.cpp @@ -1,57 +1,55 @@ -//Returns the sorted vector after performing SlowSort -//It is a sorting algorithm that is of humorous nature and not useful. -//It's based on the principle of multiply and surrender, a tongue-in-cheek joke of divide and conquer. -//It was published in 1986 by Andrei Broder and Jorge Stolfi in their paper Pessimal Algorithms and Simplexity Analysis. -//This algorithm multiplies a single problem into multiple subproblems -//It is interesting because it is provably the least efficient sorting algorithm that can be built asymptotically, -//and with the restriction that such an algorithm, while being slow, must still all the time be working towards a result. +// Returns the sorted vector after performing SlowSort +// It is a sorting algorithm that is of humorous nature and not useful. +// It's based on the principle of multiply and surrender, a tongue-in-cheek joke +// of divide and conquer. It was published in 1986 by Andrei Broder and Jorge +// Stolfi in their paper Pessimal Algorithms and Simplexity Analysis. This +// algorithm multiplies a single problem into multiple subproblems It is +// interesting because it is provably the least efficient sorting algorithm that +// can be built asymptotically, and with the restriction that such an algorithm, +// while being slow, must still all the time be working towards a result. #include using namespace std; -void SlowSort(int a[], int i, int j) -{ - if (i >= j) - return; - int m = i + (j - i) / 2; //midpoint, implemented this way to avoid overflow - int temp; - SlowSort(a, i, m); - SlowSort(a, m + 1, j); - if (a[j] < a[m]) - { - temp = a[j]; //swapping a[j] & a[m] - a[j] = a[m]; - a[m] = temp; - } - SlowSort(a, i, j - 1); +void SlowSort(int a[], int i, int j) { + if (i >= j) return; + int m = i + (j - i) / 2; // midpoint, implemented this way to avoid + // overflow + int temp; + SlowSort(a, i, m); + SlowSort(a, m + 1, j); + if (a[j] < a[m]) { + temp = a[j]; // swapping a[j] & a[m] + a[j] = a[m]; + a[m] = temp; + } + SlowSort(a, i, j - 1); } -//Sample Main function +// Sample Main function -int main() -{ - int size; - cout << "\nEnter the number of elements : "; +int main() { + int size; + cout << "\nEnter the number of elements : "; - cin >> size; + cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; - } + for (int i = 0; i < size; ++i) { + cout << "\n"; + cin >> arr[i]; + } - SlowSort(arr, 0, size); + SlowSort(arr, 0, size); - cout << "Sorted array\n"; + cout << "Sorted array\n"; - for (int i = 0; i < size; ++i) - { - cout << arr[i] << " "; - } - return 0; + for (int i = 0; i < size; ++i) { + cout << arr[i] << " "; + } + delete[] arr; + return 0; } diff --git a/sorting/swap_sort.cpp b/sorting/swap_sort.cpp index a4ab1e57b..2e59e2fb3 100644 --- a/sorting/swap_sort.cpp +++ b/sorting/swap_sort.cpp @@ -10,7 +10,7 @@ int minSwaps(int arr[], int n) { // Create an array of pairs where first // element is array element and second element // is position of first element - std::pair arrPos[n]; + std::pair *arrPos = new std::pair[n]; for (int i = 0; i < n; i++) { arrPos[i].first = arr[i]; arrPos[i].second = i; @@ -32,8 +32,7 @@ int minSwaps(int arr[], int n) { for (int i = 0; i < n; i++) { // already swapped and corrected or // already present at correct pos - if (vis[i] || arrPos[i].second == i) - continue; + if (vis[i] || arrPos[i].second == i) continue; // find out the number of node in // this cycle and add in ans @@ -53,6 +52,8 @@ int minSwaps(int arr[], int n) { } } + delete[] arrPos; + // Return result return ans; } diff --git a/sorting/tim_sort.cpp b/sorting/tim_sort.cpp index 14d3a04d0..94f5aa230 100644 --- a/sorting/tim_sort.cpp +++ b/sorting/tim_sort.cpp @@ -1,115 +1,103 @@ // C++ program to perform TimSort. +#include #include -using namespace std; + const int RUN = 32; - -// this function sorts array from left index to to right index which is of size atmost RUN -void insertionSort(int arr[], int left, int right) -{ - for (int i = left + 1; i <= right; i++) - { + +// this function sorts array from left index to to right index which is of size +// atmost RUN +void insertionSort(int arr[], int left, int right) { + for (int i = left + 1; i <= right; i++) { int temp = arr[i]; int j = i - 1; - while (arr[j] > temp && j >= left) - { - arr[j+1] = arr[j]; + while (arr[j] > temp && j >= left) { + arr[j + 1] = arr[j]; j--; } - arr[j+1] = temp; + arr[j + 1] = temp; } } - + // merge function merges the sorted runs -void merge(int arr[], int l, int m, int r) -{ +void merge(int arr[], int l, int m, int r) { // original array is broken in two parts, left and right array int len1 = m - l + 1, len2 = r - m; - int left[len1], right[len2]; - for (int i = 0; i < len1; i++) - left[i] = arr[l + i]; - for (int i = 0; i < len2; i++) - right[i] = arr[m + 1 + i]; - + int *left = new int[len1], *right = new int[len2]; + for (int i = 0; i < len1; i++) left[i] = arr[l + i]; + for (int i = 0; i < len2; i++) right[i] = arr[m + 1 + i]; + int i = 0; int j = 0; int k = l; - + // after comparing, we merge those two array in larger sub array - while (i < len1 && j < len2) - { - if (left[i] <= right[j]) - { + while (i < len1 && j < len2) { + if (left[i] <= right[j]) { arr[k] = left[i]; i++; - } - else - { + } else { arr[k] = right[j]; j++; } k++; } - + // copy remaining elements of left, if any - while (i < len1) - { + while (i < len1) { arr[k] = left[i]; k++; i++; } - + // copy remaining element of right, if any - while (j < len2) - { + while (j < len2) { arr[k] = right[j]; k++; j++; } + delete[] left; + delete[] right; } - + // iterative Timsort function to sort the array[0...n-1] (similar to merge sort) -void timSort(int arr[], int n) -{ +void timSort(int arr[], int n) { // Sort individual subarrays of size RUN - for (int i = 0; i < n; i+=RUN) - insertionSort(arr, i, min((i+31), (n-1))); - - // start merging from size RUN (or 32). It will merge to form size 64, then 128, 256 and so on .... - for (int size = RUN; size < n; size = 2*size) - { - // pick starting point of left sub array. We are going to merge arr[left..left+size-1] and arr[left+size, left+2*size-1] - // After every merge, we increase left by 2*size - for (int left = 0; left < n; left += 2*size) - { + for (int i = 0; i < n; i += RUN) + insertionSort(arr, i, std::min((i + 31), (n - 1))); + + // start merging from size RUN (or 32). It will merge to form size 64, then + // 128, 256 and so on .... + for (int size = RUN; size < n; size = 2 * size) { + // pick starting point of left sub array. We are going to merge + // arr[left..left+size-1] and arr[left+size, left+2*size-1] After every + // merge, we increase left by 2*size + for (int left = 0; left < n; left += 2 * size) { // find ending point of left sub array // mid+1 is starting point of right sub array int mid = left + size - 1; - int right = min((left + 2*size - 1), (n-1)); - + int right = std::min((left + 2 * size - 1), (n - 1)); + // merge sub array arr[left.....mid] & arr[mid+1....right] merge(arr, left, mid, right); } } } - + // utility function to print the Array -void printArray(int arr[], int n) -{ - for (int i = 0; i < n; i++) - printf("%d ", arr[i]); - printf("\n"); +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) printf("%d ", arr[i]); + std::cout << std::endl; } - + // Driver program to test above function -int main() -{ +int main() { int arr[] = {5, 21, 7, 23, 19}; - int n = sizeof(arr)/sizeof(arr[0]); + int n = sizeof(arr) / sizeof(arr[0]); printf("Given Array is\n"); printArray(arr, n); - + timSort(arr, n); - + printf("After Sorting Array is\n"); printArray(arr, n); return 0; From d3acc554c6b5f7ffa1a12ffb49d0323d852a3c20 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 13:07:14 -0400 Subject: [PATCH 17/28] cpplint issues fixed in sorting folder (cherry picked from commit 35c53760d34bc483ba737706937985346bf39fd8) --- sorting/radix_sort.cpp | 2 +- sorting/slow_sort.cpp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sorting/radix_sort.cpp b/sorting/radix_sort.cpp index e5bc3db84..a0fbfe99e 100644 --- a/sorting/radix_sort.cpp +++ b/sorting/radix_sort.cpp @@ -55,4 +55,4 @@ int main(int argc, char const* argv[]) { radixsort(a, n); print(a, n); return 0; -} \ No newline at end of file +} diff --git a/sorting/slow_sort.cpp b/sorting/slow_sort.cpp index 2872b37d8..b2627d12f 100644 --- a/sorting/slow_sort.cpp +++ b/sorting/slow_sort.cpp @@ -9,7 +9,6 @@ // while being slow, must still all the time be working towards a result. #include -using namespace std; void SlowSort(int a[], int i, int j) { if (i >= j) return; @@ -30,26 +29,27 @@ void SlowSort(int a[], int i, int j) { int main() { int size; - cout << "\nEnter the number of elements : "; + std::cout << "\nEnter the number of elements : "; - cin >> size; + std::cin >> size; int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + std::cout << "\nEnter the unsorted elements : "; for (int i = 0; i < size; ++i) { - cout << "\n"; - cin >> arr[i]; + std::cout << "\n"; + std::cin >> arr[i]; } SlowSort(arr, 0, size); - cout << "Sorted array\n"; + std::cout << "Sorted array\n"; for (int i = 0; i < size; ++i) { - cout << arr[i] << " "; + std::cout << arr[i] << " "; } + delete[] arr; return 0; } From 6e6f92e7739526bda9772c0edcc433b01062fac3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 May 2020 17:33:50 +0000 Subject: [PATCH 18/28] updating DIRECTORY.md --- DIRECTORY.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 47842b9ca..9a336c1a5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -179,29 +179,29 @@ * [Ternary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/ternary_search.cpp) ## Sorting - * [Beadsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/BeadSort.cpp) - * [Bitonicsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/BitonicSort.cpp) - * [Bubble Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Bubble%20Sort.cpp) - * [Bucketsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bucketSort.cpp) - * [Cocktailselectionsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/CocktailSelectionSort.cpp) - * [Combsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/combsort.cpp) + * [Bead Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bead_sort.cpp) + * [Bitonic Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bitonic_sort.cpp) + * [Bubble Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bubble_sort.cpp) + * [Bucket Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bucket_sort.cpp) + * [Cocktail Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cocktail_selection_sort.cpp) + * [Comb Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/comb_sort.cpp) * [Counting Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Counting_Sort.cpp) - * [Countingsortstring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/CountingSortString.cpp) + * [Counting Sort String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort_string.cpp) * [Heap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/heap_sort.cpp) - * [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Insertion%20Sort.cpp) + * [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/insertion_sort.cpp) * [Library Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/library_sort.cpp) - * [Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Merge%20Sort.cpp) + * [Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/merge_sort.cpp) * [Non Recursive Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/non_recursive_merge_sort.cpp) - * [Numericstringsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/NumericStringSort.cpp) - * [Oddeven Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/OddEven%20Sort.cpp) - * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Quick%20Sort.cpp) - * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) - * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) - * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp) + * [Numeric String Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/numeric_string_sort.cpp) + * [Odd Even Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/odd_even_sort.cpp) + * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) + * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/radix_sort.cpp) + * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/selection_sort.cpp) + * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort.cpp) * [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp) - * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp) + * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/slow_sort.cpp) * [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp) - * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp) + * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/tim_sort.cpp) ## Strings * [Brute Force String Searching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/strings/brute_force_string_searching.cpp) From 07ebe713b01d26bd32b0dc1c8965459e7837f390 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 13:41:11 -0400 Subject: [PATCH 19/28] added deleted files back - not really needed but there is a redundant github action that required it --- sorting/doxy.txt | 374 +++++++++++++++++++++++++++++++++++++++++++++++ sorting/makefile | 11 ++ 2 files changed, 385 insertions(+) create mode 100644 sorting/doxy.txt create mode 100644 sorting/makefile diff --git a/sorting/doxy.txt b/sorting/doxy.txt new file mode 100644 index 000000000..68079276e --- /dev/null +++ b/sorting/doxy.txt @@ -0,0 +1,374 @@ +# Doxyfile 1.8.13 +#This configuration file has been generated from Doxygen template. +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "My Project" +PROJECT_NUMBER = +PROJECT_BRIEF = +PROJECT_LOGO = +OUTPUT_DIRECTORY = +CREATE_SUBDIRS = NO +ALLOW_UNICODE_NAMES = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +QT_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 4 +ALIASES = +TCL_SUBST = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = NO +OPTIMIZE_FOR_FORTRAN = NO +OPTIMIZE_OUTPUT_VHDL = NO +EXTENSION_MAPPING = +MARKDOWN_SUPPORT = YES +TOC_INCLUDE_HEADINGS = 0 +AUTOLINK_SUPPORT = YES +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +IDL_PROPERTY_SUPPORT = YES +DISTRIBUTE_GROUP_DOC = NO +GROUP_NESTED_COMPOUNDS = NO +SUBGROUPING = YES +INLINE_GROUPED_CLASSES = NO +INLINE_SIMPLE_STRUCTS = NO +TYPEDEF_HIDES_STRUCT = NO +LOOKUP_CACHE_SIZE = 0 +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_PACKAGE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +HIDE_COMPOUND_REFERENCE= NO +SHOW_INCLUDE_FILES = YES +SHOW_GROUPED_MEMB_INC = NO +FORCE_LOCAL_INCLUDES = NO +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = NO +SORT_MEMBERS_CTORS_1ST = NO +SORT_GROUP_NAMES = NO +SORT_BY_SCOPE_NAME = NO +STRICT_PROTO_MATCHING = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_FILES = YES +SHOW_NAMESPACES = YES +FILE_VERSION_FILTER = +LAYOUT_FILE = +CITE_BIB_FILES = +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_AS_ERROR = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.idl \ + *.ddl \ + *.odl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.cs \ + *.d \ + *.php \ + *.php4 \ + *.php5 \ + *.phtml \ + *.inc \ + *.m \ + *.markdown \ + *.md \ + *.mm \ + *.dox \ + *.py \ + *.pyw \ + *.f90 \ + *.f95 \ + *.f03 \ + *.f08 \ + *.f \ + *.for \ + *.tcl \ + *.vhd \ + *.vhdl \ + *.ucf \ + *.qsf +RECURSIVE = NO +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +FILTER_SOURCE_PATTERNS = +USE_MDFILE_AS_MAINPAGE = +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = NO +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = NO +REFERENCES_RELATION = NO +REFERENCES_LINK_SOURCE = YES +SOURCE_TOOLTIPS = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +CLANG_ASSISTED_PARSING = NO +CLANG_OPTIONS = +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_EXTRA_STYLESHEET = +HTML_EXTRA_FILES = +HTML_COLORSTYLE_HUE = 220 +HTML_COLORSTYLE_SAT = 100 +HTML_COLORSTYLE_GAMMA = 80 +HTML_TIMESTAMP = NO +HTML_DYNAMIC_SECTIONS = NO +HTML_INDEX_NUM_ENTRIES = 100 +GENERATE_DOCSET = NO +DOCSET_FEEDNAME = "Doxygen generated docs" +DOCSET_BUNDLE_ID = org.doxygen.Project +DOCSET_PUBLISHER_ID = org.doxygen.Publisher +DOCSET_PUBLISHER_NAME = Publisher +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +CHM_INDEX_ENCODING = +BINARY_TOC = NO +TOC_EXPAND = NO +GENERATE_QHP = NO +QCH_FILE = +QHP_NAMESPACE = org.doxygen.Project +QHP_VIRTUAL_FOLDER = doc +QHP_CUST_FILTER_NAME = +QHP_CUST_FILTER_ATTRS = +QHP_SECT_FILTER_ATTRS = +QHG_LOCATION = +GENERATE_ECLIPSEHELP = NO +ECLIPSE_DOC_ID = org.doxygen.Project +DISABLE_INDEX = NO +GENERATE_TREEVIEW = NO +ENUM_VALUES_PER_LINE = 4 +TREEVIEW_WIDTH = 250 +EXT_LINKS_IN_WINDOW = NO +FORMULA_FONTSIZE = 10 +FORMULA_TRANSPARENT = YES +USE_MATHJAX = NO +MATHJAX_FORMAT = HTML-CSS +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest +MATHJAX_EXTENSIONS = +MATHJAX_CODEFILE = +SEARCHENGINE = YES +SERVER_BASED_SEARCH = NO +EXTERNAL_SEARCH = NO +SEARCHENGINE_URL = +SEARCHDATA_FILE = searchdata.xml +EXTERNAL_SEARCH_ID = +EXTRA_SEARCH_MAPPINGS = +#--------------------------------------------------------------------------- +# Configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4 +EXTRA_PACKAGES = +LATEX_HEADER = +LATEX_FOOTER = +LATEX_EXTRA_STYLESHEET = +LATEX_EXTRA_FILES = +PDF_HYPERLINKS = YES +USE_PDFLATEX = YES +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +LATEX_SOURCE_CODE = NO +LATEX_BIB_STYLE = plain +LATEX_TIMESTAMP = NO +#--------------------------------------------------------------------------- +# Configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +RTF_SOURCE_CODE = NO +#--------------------------------------------------------------------------- +# Configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_SUBDIR = +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# Configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# Configuration options related to the DOCBOOK output +#--------------------------------------------------------------------------- +GENERATE_DOCBOOK = NO +DOCBOOK_OUTPUT = docbook +DOCBOOK_PROGRAMLISTING = NO +#--------------------------------------------------------------------------- +# Configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# Configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration options related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +EXTERNAL_PAGES = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +DIA_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +DOT_NUM_THREADS = 0 +DOT_FONTNAME = Helvetica +DOT_FONTSIZE = 10 +DOT_FONTPATH = +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +UML_LIMIT_NUM_FIELDS = 10 +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +INTERACTIVE_SVG = NO +DOT_PATH = +DOTFILE_DIRS = +MSCFILE_DIRS = +DIAFILE_DIRS = +PLANTUML_JAR_PATH = +PLANTUML_CFG_FILE = +PLANTUML_INCLUDE_PATH = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 0 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES diff --git a/sorting/makefile b/sorting/makefile new file mode 100644 index 000000000..21d3186c6 --- /dev/null +++ b/sorting/makefile @@ -0,0 +1,11 @@ +non_recursive_merge_sort: non_recursive_merge_sort.cpp + g++ -std=c++17 -o non_recursive_merge_sort non_recursive_merge_sort.cpp +.PHONY: test +.PHONY: doc +.PHONY: clean +test: non_recursive_merge_sort + ./non_recursive_merge_sort +doc: doxy.txt + doxygen doxy.txt +clean: + rm -fr non_recursive_merge_sort html From 030348a284415e04315b4ee865da47d7bce74fdd Mon Sep 17 00:00:00 2001 From: liushubin-gitHub <65377959+liushubin-gitHub@users.noreply.github.com> Date: Wed, 27 May 2020 10:03:54 +0800 Subject: [PATCH 20/28] Update doubly_linked_list.cpp when remove an item , should free the item memory --- data_structure/doubly_linked_list.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index 17ea2983a..4fc38abfc 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -61,6 +61,7 @@ void double_linked_list::remove(int x) { t->prev->next = t->next; t->next->prev = t->prev; } + delete t; } void double_linked_list::search(int x) { From ee3547fafcbf48650b0ae6eb92fe6a9809bb2b93 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 27 May 2020 15:41:15 +0530 Subject: [PATCH 21/28] Correction : Fixed Array Overflow --- sorting/quick_sort.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sorting/quick_sort.cpp diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp new file mode 100644 index 000000000..12ee66456 --- /dev/null +++ b/sorting/quick_sort.cpp @@ -0,0 +1,61 @@ +/* + * + * copyright The Algorithms + * Author - + * Correction - ayaankhan98 + * + */ + +#include +#include + +int partition(int arr[], int low, int high) { + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element + + for (int j = low; j < high; j++) { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) { + i++; // increment index of smaller element + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + return (i + 1); +} + +void quickSort(int arr[], int low, int high) { + if (low < high) { + int p = partition(arr, low, high); + quickSort(arr, low, p - 1); + quickSort(arr, p + 1, high); + } +} + +void show(int arr[], int size) { + for (int i = 0; i < size; i++) + std::cout << arr[i] << " "; + std::cout << "\n"; +} + +// Driver program to test above functions + +int main() { + int size; + std::cout << "\nEnter the number of elements : "; + std::cin >> size; + int *arr = new int[size]; + std::cout << "\nEnter the unsorted elements : "; + for (int i = 0; i < size; ++i) { + std::cin >> arr[i]; + } + quickSort(arr, 0, size-1); + std::cout << "Sorted array : "; + show(arr, size); + return 0; +} From d86ff19d44ad919abb3f4cb64d9431300f5dded8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 27 May 2020 12:14:45 +0000 Subject: [PATCH 22/28] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e36309dc..636543a25 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -197,6 +197,7 @@ * [Numericstringsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/NumericStringSort.cpp) * [Oddeven Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/OddEven%20Sort.cpp) * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Quick%20Sort.cpp) + * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp) From c2cde125dd8c9077a10c8a5b29dfd00933ec5adb Mon Sep 17 00:00:00 2001 From: Ayaan Khan <43348292+ayaankhan98@users.noreply.github.com> Date: Wed, 27 May 2020 18:54:19 +0530 Subject: [PATCH 23/28] Added documentation (#802) * Added documentation * Added breif comments on functions * modified comment block * further improved comment blocks --- sorting/Quick Sort.cpp | 67 ------------------------------------------ sorting/quick_sort.cpp | 38 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 70 deletions(-) delete mode 100644 sorting/Quick Sort.cpp diff --git a/sorting/Quick Sort.cpp b/sorting/Quick Sort.cpp deleted file mode 100644 index 0b807898f..000000000 --- a/sorting/Quick Sort.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* C implementation QuickSort */ -#include -using namespace std; - -int partition(int arr[], int low, int high) -{ - int pivot = arr[high]; // pivot - int i = (low - 1); // Index of smaller element - - for (int j = low; j < high; j++) - { - // If current element is smaller than or - // equal to pivot - if (arr[j] <= pivot) - { - i++; // increment index of smaller element - int temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - } - int temp = arr[i + 1]; - arr[i + 1] = arr[high]; - arr[high] = temp; - return (i + 1); -} - -void quickSort(int arr[], int low, int high) -{ - if (low < high) - { - - int p = partition(arr, low, high); - - quickSort(arr, low, p - 1); - quickSort(arr, p + 1, high); - } -} - -void show(int arr[], int size) -{ - for (int i = 0; i < size; i++) - cout << arr[i] << "\n"; -} - -// Driver program to test above functions -int main() -{ - int size; - cout << "\nEnter the number of elements : "; - - cin >> size; - - int arr[size]; - - cout << "\nEnter the unsorted elements : "; - - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; - } - quickSort(arr, 0, size); - cout << "Sorted array\n"; - show(arr, size); - return 0; -} diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 12ee66456..d067fa068 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -1,16 +1,42 @@ -/* +/** * * copyright The Algorithms * Author - * Correction - ayaankhan98 * + * Implementation Details - + * Quick Sort is a divide and conquer algorithm. It picks and element as + * pivot and partition the given array around the picked pivot. There + * are many different versions of quickSort that pick pivot in different + * ways. + * + * 1. Always pick the first element as pivot + * 2. Always pick the last element as pivot (implemented below) + * 3. Pick a random element as pivot + * 4. Pick median as pivot + * + * The key process in quickSort is partition(). Target of partition is, + * given an array and an element x(say) of array as pivot, put x at it's + * correct position in sorted array and put all smaller elements (samller + * than x) before x, and put all greater elements (greater than x) after + * x. All this should be done in linear time + * */ #include #include +/** + * This function takes last element as pivot, places + * the pivot element at its correct position in sorted + * array, and places all smaller (smaller than pivot) + * to left of pivot and all greater elements to right + * of pivot + * + */ + int partition(int arr[], int low, int high) { - int pivot = arr[high]; // pivot + int pivot = arr[high]; // taking the last element as pivot int i = (low - 1); // Index of smaller element for (int j = low; j < high; j++) { @@ -29,6 +55,12 @@ int partition(int arr[], int low, int high) { return (i + 1); } +/** + * The main function that implements QuickSort + * arr[] --> Array to be sorted, + * low --> Starting index, + * high --> Ending index +*/ void quickSort(int arr[], int low, int high) { if (low < high) { int p = partition(arr, low, high); @@ -37,6 +69,7 @@ void quickSort(int arr[], int low, int high) { } } +// prints the array after sorting void show(int arr[], int size) { for (int i = 0; i < size; i++) std::cout << arr[i] << " "; @@ -44,7 +77,6 @@ void show(int arr[], int size) { } // Driver program to test above functions - int main() { int size; std::cout << "\nEnter the number of elements : "; From 5a54093484588b2a11e96b40dca22e3a0ee42e8a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 27 May 2020 13:24:42 +0000 Subject: [PATCH 24/28] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 636543a25..9fdf260a9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -196,7 +196,6 @@ * [Non Recursive Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/non_recursive_merge_sort.cpp) * [Numericstringsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/NumericStringSort.cpp) * [Oddeven Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/OddEven%20Sort.cpp) - * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Quick%20Sort.cpp) * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) From e963ba4c5b22f0e0984c60d474d329e88e761f93 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 18:00:02 -0400 Subject: [PATCH 25/28] fix buzz number --- others/buzz_number.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/others/buzz_number.cpp b/others/buzz_number.cpp index f31038aad..ed9fc5f28 100644 --- a/others/buzz_number.cpp +++ b/others/buzz_number.cpp @@ -1,17 +1,20 @@ -//A buzz number is a number that is either divisble by 7 or has last digit as 7. +/** + * @file + * @brief A buzz number is a number that is either divisible by 7 or has last + * digit as 7. + */ #include -using namespace std; -int main() -{ - int n, t; - cin >> t; - while (t--) - { - cin >> n; - if ((n % 7 == 0) || (n % 10 == 7)) - cout << n << " is a buzz number" << endl; - else - cout << n << " is not a buzz number" << endl; - } - return 0; + +/** main function */ +int main() { + int n, t; + std::cin >> t; + while (t--) { + std::cin >> n; + if ((n % 7 == 0) || (n % 10 == 7)) + std::cout << n << " is a buzz number" << std::endl; + else + std::cout << n << " is not a buzz number" << std::endl; + } + return 0; } From e7632d107caddbf7928f872f509678e678a5262c Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 18:16:32 -0400 Subject: [PATCH 26/28] fixed CPPLINT + added method 2 --- others/decimal_to_binary.cpp | 72 +++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/others/decimal_to_binary.cpp b/others/decimal_to_binary.cpp index 4e2119ebc..11ce064a5 100644 --- a/others/decimal_to_binary.cpp +++ b/others/decimal_to_binary.cpp @@ -1,25 +1,55 @@ -// This function convert decimal to binary number -// +/** + * @file + * @brief Function to convert decimal number to binary representation + */ #include -using namespace std; -int main() -{ - int number; - cout << "Enter a number:"; - cin >> number; - int remainder, binary = 0, var = 1; +/** + * This method converts the bit representation and stores it as a decimal + * number. + */ +void method1(int number) { + int remainder, binary = 0, var = 1; - do - { - remainder = number % 2; - number = number / 2; - binary = binary + (remainder * var); - var = var * 10; - - } while (number > 0); - cout << "the binary is :"; - cout << binary; - cout << endl; - return 0; + do { + remainder = number % 2; + number = number / 2; + binary = binary + (remainder * var); + var = var * 10; + } while (number > 0); + std::cout << "Method 1 : " << binary << std::endl; +} + +/** + * This method stores each bit value from LSB to MSB and then prints them back + * from MSB to LSB + */ +void method2(int number) { + int num_bits = 0; + char bit_string[50]; + + do { + bool bit = number & 0x01; // get last bit + if (bit) + bit_string[num_bits++] = '1'; + else + bit_string[num_bits++] = '0'; + number >>= 1; // right shift bit 1 bit + } while (number > 0); + + std::cout << "Method 2 : "; + while (num_bits >= 0) + std::cout << bit_string[num_bits--]; // print from MSB to LSB + std::cout << std::endl; +} + +int main() { + int number; + std::cout << "Enter a number:"; + std::cin >> number; + + method1(number); + method2(number); + + return 0; } From 7e875baab71de9b02537e7d9a1075ec9bdc4ccc3 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 18:22:49 -0400 Subject: [PATCH 27/28] fixed decimal to hex --- others/decimal_to_hexadecimal.cpp | 48 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/others/decimal_to_hexadecimal.cpp b/others/decimal_to_hexadecimal.cpp index 705f21ba4..a3e544f49 100644 --- a/others/decimal_to_hexadecimal.cpp +++ b/others/decimal_to_hexadecimal.cpp @@ -1,28 +1,34 @@ +/** + * @file + * @brief Convert decimal number to hexadecimal representation + */ + #include -using namespace std; +/** + * Main program + */ +int main(void) { + int valueToConvert = 0; // Holds user input + int hexArray[8]; // Contains hex values backwards + int i = 0; // counter + char HexValues[] = "0123456789ABCDEF"; -int main(void) -{ - int valueToConvert = 0; //Holds user input - int hexArray[8]; //Contains hex values backwards - int i = 0; //counter - char HexValues[] = "0123456789ABCDEF"; + std::cout << "Enter a Decimal Value" + << std::endl; // Displays request to stdout + std::cin >> + valueToConvert; // Stores value into valueToConvert via user input - cout << "Enter a Decimal Value" << endl; //Displays request to stdout - cin >> valueToConvert; //Stores value into valueToConvert via user input + while (valueToConvert > 15) { // Dec to Hex Algorithm + hexArray[i++] = valueToConvert % 16; // Gets remainder + valueToConvert /= 16; + // valueToConvert >>= 4; // This will divide by 2^4=16 and is faster + } + hexArray[i] = valueToConvert; // Gets last value - while (valueToConvert > 15) - { //Dec to Hex Algorithm - hexArray[i++] = valueToConvert % 16; //Gets remainder - valueToConvert /= 16; - } - hexArray[i] = valueToConvert; //Gets last value + std::cout << "Hex Value: "; + while (i >= 0) std::cout << HexValues[hexArray[i--]]; - cout << "Hex Value: "; - while (i >= 0) - cout << HexValues[hexArray[i--]]; - - cout << endl; - return 0; + std::cout << std::endl; + return 0; } From a8bdbbda355be055f5d99fa7e3dfcd71ea9a0ba9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 18:22:57 -0400 Subject: [PATCH 28/28] fixed decimal to roman --- others/decimal_to_roman_numeral.cpp | 131 ++++++++++++---------------- 1 file changed, 55 insertions(+), 76 deletions(-) diff --git a/others/decimal_to_roman_numeral.cpp b/others/decimal_to_roman_numeral.cpp index 0372e8003..6bd1be395 100644 --- a/others/decimal_to_roman_numeral.cpp +++ b/others/decimal_to_roman_numeral.cpp @@ -1,97 +1,76 @@ -//This Programme Converts a given decimal number in the range [0,4000) -//to both Lower case and Upper case Roman Numeral +/** + * @file + * @brief This Programme Converts a given decimal number in the range [0,4000) + * to both Lower case and Upper case Roman Numeral + */ #include #include -#include +#include #include -using namespace std; -//This functions fills a string with character c, n times and returns it -string fill(char c, int n) -{ - string s = ""; - while (n--) - s += c; +/** This functions fills a string with character c, n times and returns it + * @note This can probably be replace by `memcpy` function. + */ +std::string fill(char c, int n) { + std::string s = ""; + while (n--) s += c; return s; } -//to convert to lowercase Roman Numeral -// the function works recursively -string tolowerRoman(int n) -{ - if (n < 4) - return fill('i', n); - if (n < 6) - return fill('i', 5 - n) + "v"; - if (n < 9) - return string("v") + fill('i', n - 5); - if (n < 11) - return fill('i', 10 - n) + "x"; - if (n < 40) - return fill('x', n / 10) + tolowerRoman(n % 10); - if (n < 60) - return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10); +/** to convert to lowercase Roman Numeral + * the function works recursively + */ +std::string tolowerRoman(int n) { + if (n < 4) return fill('i', n); + if (n < 6) return fill('i', 5 - n) + "v"; + if (n < 9) return std::string("v") + fill('i', n - 5); + if (n < 11) return fill('i', 10 - n) + "x"; + if (n < 40) return fill('x', n / 10) + tolowerRoman(n % 10); + if (n < 60) return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10); if (n < 90) - return string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10); - if (n < 110) - return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10); - if (n < 400) - return fill('c', n / 100) + tolowerRoman(n % 100); - if (n < 600) - return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100); + return std::string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10); + if (n < 110) return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10); + if (n < 400) return fill('c', n / 100) + tolowerRoman(n % 100); + if (n < 600) return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100); if (n < 900) - return string("d") + fill('c', n / 100 - 5) + tolowerRoman(n % 100); - if (n < 1100) - return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100); - if (n < 4000) - return fill('m', n / 1000) + tolowerRoman(n % 1000); + return std::string("d") + fill('c', n / 100 - 5) + + tolowerRoman(n % 100); + if (n < 1100) return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100); + if (n < 4000) return fill('m', n / 1000) + tolowerRoman(n % 1000); return "?"; } -//to convert to uppercase Roman Numeral -// the function works recursively -string toupperRoman(int n) -{ - if (n < 4) - return fill('I', n); - if (n < 6) - return fill('I', 5 - n) + "V"; - if (n < 9) - return string("V") + fill('I', n - 5); - if (n < 11) - return fill('I', 10 - n) + "X"; - if (n < 40) - return fill('X', n / 10) + toupperRoman(n % 10); - if (n < 60) - return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10); +/** to convert to uppercase Roman Numeral + * the function works recursively + */ +std::string toupperRoman(int n) { + if (n < 4) return fill('I', n); + if (n < 6) return fill('I', 5 - n) + "V"; + if (n < 9) return std::string("V") + fill('I', n - 5); + if (n < 11) return fill('I', 10 - n) + "X"; + if (n < 40) return fill('X', n / 10) + toupperRoman(n % 10); + if (n < 60) return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10); if (n < 90) - return string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10); - if (n < 110) - return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10); - if (n < 400) - return fill('C', n / 100) + toupperRoman(n % 100); - if (n < 600) - return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100); + return std::string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10); + if (n < 110) return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10); + if (n < 400) return fill('C', n / 100) + toupperRoman(n % 100); + if (n < 600) return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100); if (n < 900) - return string("D") + fill('C', n / 100 - 5) + toupperRoman(n % 100); - if (n < 1100) - return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100); - if (n < 4000) - return fill('M', n / 1000) + toupperRoman(n % 1000); + return std::string("D") + fill('C', n / 100 - 5) + + toupperRoman(n % 100); + if (n < 1100) return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100); + if (n < 4000) return fill('M', n / 1000) + toupperRoman(n % 1000); return "?"; } -//main function - -int main() -{ - +/** main function */ +int main() { int n; - cout << "\t\tRoman numbers converter\n\n"; - cout << "Type in decimal number between 0 up to 4000 (exclusive): "; - cin >> n; - cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n"; - cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n"; + std::cout << "\t\tRoman numbers converter\n\n"; + std::cout << "Type in decimal number between 0 up to 4000 (exclusive): "; + std::cin >> n; + std::cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n"; + std::cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n"; return 0; }