diff --git a/.github/workflows/cpplint_modified_files.yml b/.github/workflows/cpplint_modified_files.yml index 07a32ece97d..8de6a5e4115 100644 --- a/.github/workflows/cpplint_modified_files.yml +++ b/.github/workflows/cpplint_modified_files.yml @@ -11,7 +11,7 @@ jobs: cpplint_modified_files: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 # v2 is broken for git diff + - uses: actions/checkout@v1 # v2 is broken for git diff - uses: actions/setup-python@v1 - run: python -m pip install cpplint - run: git remote -v @@ -42,7 +42,8 @@ jobs: print("g++:") # compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split()) # compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)] - subprocess.run(["g++"] + cpp_files, check=True, text=True) + for cpp_file in cpp_files: + subprocess.run(["g++", cpp_file], check=True, text=True) upper_files = [file for file in cpp_files if file != file.lower()] if upper_files: diff --git a/.gitignore b/.gitignore index ed3934e45be..5275088d0f4 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ a.out *.out *.app + +build/ diff --git a/DIRECTORY.md b/DIRECTORY.md index 9fdf260a92f..9a336c1a5b1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,13 +10,13 @@ * [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp) ## Computer Oriented Statistical Methods - * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP) - * [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp) - * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp) - * [Newton Raphson](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Newton_Raphson.CPP) + * [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp) + * [False Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false_position.cpp) + * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/gaussian_elimination.cpp) + * [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp) * [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp) - * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP) - * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP) + * [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp) + * [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.cpp) ## Data Structure * [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp) @@ -116,7 +116,7 @@ * [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp) * [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp) * [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp) - * [Primes Up To 10^8](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_10^8.cpp) + * [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp) * [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp) @@ -132,28 +132,26 @@ * [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/Union_of_2_arrays.cpp) ## Others - * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp) - * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Binary.cpp) - * [Decimal To Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp) - * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.cpp) + * [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/buzz_number.cpp) + * [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp) + * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp) + * [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp) * [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp) - * [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci.cpp) - * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.cpp) + * [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp) + * [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) - * [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Paranthesis%20Matching.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%20Test.cpp) + * [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp) * [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp) - * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Sparse%20matrix.cpp) + * [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sparse_matrix.cpp) * [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp) * [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp) - * [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Strassen%20Matrix%20Multiplication.cpp) - * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/String%20Fibonacci.cpp) - * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp) + * [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp) + * [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp) * [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp) ## Probability @@ -181,28 +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) + * [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%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) - * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.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_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) diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/Bisection_method.CPP deleted file mode 100644 index 717987321cd..00000000000 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} - -void main() -{ - float a, b, x, z; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - x = (a + b) / 2; - z = eq(x); - cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]"; - - if (z < 0) - { - a = x; - } - else - { - b = x; - } - - if (z > 0 && z < 0.0009) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << x; - getch(); -} diff --git a/computer_oriented_statistical_methods/Gaussian_elimination.cpp b/computer_oriented_statistical_methods/Gaussian_elimination.cpp deleted file mode 100644 index 58e634505af..00000000000 --- a/computer_oriented_statistical_methods/Gaussian_elimination.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include -using namespace std; - -int main() -{ - int mat_size, i, j, step; - - cout << "Matrix size: "; - cin >> mat_size; - - double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1]; - - cout << endl - << "Enter value of the matrix: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { - cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix. - } - } - - for (step = 0; step < mat_size - 1; step++) - { - for (i = step; i < mat_size - 1; i++) - { - double a = (mat[i + 1][step] / mat[step][step]); - - for (j = step; j <= mat_size; j++) - mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]); - } - } - - cout << endl - << "Matrix using Gaussian Elimination method: " << endl; - for (i = 0; i < mat_size; i++) - { - for (j = 0; j <= mat_size; j++) - { - x[i][j] = mat[i][j]; - cout << mat[i][j] << " "; - } - cout << endl; - } - cout << endl - << "Value of the Gaussian Elimination method: " << endl; - for (i = mat_size - 1; i >= 0; i--) - { - double sum = 0; - for (j = mat_size - 1; j > i; j--) - { - x[i][j] = x[j][j] * x[i][j]; - sum = x[i][j] + sum; - } - if (x[i][i] == 0) - x[i][i] = 0; - else - x[i][i] = (x[i][mat_size] - sum) / (x[i][i]); - - cout << "x" << i << "= " << x[i][i] << endl; - } - return 0; -} diff --git a/computer_oriented_statistical_methods/Newton_Raphson.CPP b/computer_oriented_statistical_methods/Newton_Raphson.CPP deleted file mode 100644 index 183a78daf8a..00000000000 --- a/computer_oriented_statistical_methods/Newton_Raphson.CPP +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} -float eq_der(float i) -{ - return ((3 * pow(i, 2)) - 4); // derivative of equation -} - -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - c = (a + b) / 2; - - for (i = 0; i < 100; i++) - { - float h; - m = eq(c); - n = eq_der(c); - - z = c - (m / n); - c = z; - - if (m > 0 && m < 0.009) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << z; - getch(); -} diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/Secant_method.CPP deleted file mode 100644 index b897e9184f4..00000000000 --- a/computer_oriented_statistical_methods/Secant_method.CPP +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} - -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - float h, d; - m = eq(a); - n = eq(b); - - c = ((a * n) - (b * m)) / (n - m); - a = b; - b = c; - - z = eq(c); - if (z > 0 && z < 0.09) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << c; - getch(); -} diff --git a/computer_oriented_statistical_methods/bisection_method.cpp b/computer_oriented_statistical_methods/bisection_method.cpp new file mode 100644 index 00000000000..089828c9a3f --- /dev/null +++ b/computer_oriented_statistical_methods/bisection_method.cpp @@ -0,0 +1,40 @@ +#include +#include + +static float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation +} + +int main() { + float a, b, x, z; + + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + x = (a + b) / 2; + z = eq(x); + std::cout << "\n\nz: " << z << "\t[" << a << " , " << b + << " | Bisect: " << x << "]"; + + if (z < 0) { + a = x; + } else { + b = x; + } + + if (z > 0 && z < 0.0009) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << x; + return 0; +} diff --git a/computer_oriented_statistical_methods/false-position.cpp b/computer_oriented_statistical_methods/false_position.cpp similarity index 80% rename from computer_oriented_statistical_methods/false-position.cpp rename to computer_oriented_statistical_methods/false_position.cpp index 5e15e92cc4c..c5a31450868 100644 --- a/computer_oriented_statistical_methods/false-position.cpp +++ b/computer_oriented_statistical_methods/false_position.cpp @@ -1,9 +1,11 @@ -#include -#include +#include +#include #include -float eq(float i) { + +static float eq(float i) { return (pow(i, 3) - (4 * i) - 9); // origial equation } + int main() { float a, b, z, c, m, n; system("clear"); @@ -12,12 +14,13 @@ int main() { if (z >= 0) { b = i; a = --i; - goto START; - } + break; } - START: + } + std::cout << "\nFirst initial: " << a; std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { float h, d; m = eq(a); @@ -26,10 +29,10 @@ int main() { a = c; z = eq(c); if (z > 0 && z < 0.09) { // stoping criteria - goto END; + break; } } - END: + std::cout << "\n\nRoot: " << c; - system("pause"); + return 0; } diff --git a/computer_oriented_statistical_methods/gaussian_elimination.cpp b/computer_oriented_statistical_methods/gaussian_elimination.cpp new file mode 100644 index 00000000000..0b8bb693de6 --- /dev/null +++ b/computer_oriented_statistical_methods/gaussian_elimination.cpp @@ -0,0 +1,65 @@ +#include + +int main() { + int mat_size, i, j, step; + + std::cout << "Matrix size: "; + std::cin >> mat_size; + + 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++) { + for (j = 0; j <= mat_size; j++) { + std::cin >> + mat[i][j]; // Enter (mat_size*mat_size) value of the matrix. + } + } + + for (step = 0; step < mat_size - 1; step++) { + for (i = step; i < mat_size - 1; i++) { + double a = (mat[i + 1][step] / mat[step][step]); + + for (j = step; j <= mat_size; j++) + mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]); + } + } + + std::cout << std::endl + << "Matrix using Gaussian Elimination method: " << std::endl; + for (i = 0; i < mat_size; i++) { + for (j = 0; j <= mat_size; j++) { + x[i][j] = mat[i][j]; + std::cout << mat[i][j] << " "; + } + std::cout << std::endl; + } + std::cout << std::endl + << "Value of the Gaussian Elimination method: " << std::endl; + for (i = mat_size - 1; i >= 0; i--) { + double sum = 0; + for (j = mat_size - 1; j > i; j--) { + x[i][j] = x[j][j] * x[i][j]; + sum = x[i][j] + sum; + } + if (x[i][i] == 0) + x[i][i] = 0; + else + x[i][i] = (x[i][mat_size] - sum) / (x[i][i]); + + 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; +} diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp new file mode 100644 index 00000000000..47d2764908b --- /dev/null +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -0,0 +1,42 @@ +#include +#include + +static float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation +} + +static float eq_der(float i) { + return ((3 * std::pow(i, 2)) - 4); // derivative of equation +} + +int main() { + float a, b, z, c, m, n; + + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + c = (a + b) / 2; + + for (int i = 0; i < 100; i++) { + float h; + m = eq(c); + n = eq_der(c); + + z = c - (m / n); + c = z; + + if (m > 0 && m < 0.009) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << z << std::endl; + return 0; +} diff --git a/computer_oriented_statistical_methods/secant_method.cpp b/computer_oriented_statistical_methods/secant_method.cpp new file mode 100644 index 00000000000..c353ef850f4 --- /dev/null +++ b/computer_oriented_statistical_methods/secant_method.cpp @@ -0,0 +1,37 @@ +#include +#include + +static float eq(float i) { + return (pow(i, 3) - (4 * i) - 9); // original equation +} + +int main() { + float a, b, z, c, m, n; + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + float h, d; + m = eq(a); + n = eq(b); + + c = ((a * n) - (b * m)) / (n - m); + a = b; + b = c; + + z = eq(c); + if (z > 0 && z < 0.09) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << c; + return 0; +} diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.CPP deleted file mode 100644 index b42ab8f8c11..00000000000 --- a/computer_oriented_statistical_methods/successive_approximation.CPP +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -float eq(float y) -{ - return ((3 * y) - (cos(y)) - 2); -} -float eqd(float y) -{ - return ((0.5) * ((cos(y)) + 2)); -} - -void main() -{ - float y, x1, x2, x3, sum, s, a, f1, f2, gd; - int i, n; - - clrscr(); - for (i = 0; i < 10; i++) - { - sum = eq(y); - cout << "value of equation at " << i << " " << sum << "\n"; - y++; - } - cout << "enter the x1->"; - cin >> x1; - cout << "enter the no iteration to perform->\n"; - cin >> n; - - for (i = 0; i <= n; i++) - { - x2 = eqd(x1); - cout << "\nenter the x2->" << x2; - x1 = x2; - } - getch(); -} \ No newline at end of file diff --git a/computer_oriented_statistical_methods/successive_approximation.cpp b/computer_oriented_statistical_methods/successive_approximation.cpp new file mode 100644 index 00000000000..efbcc3bbff3 --- /dev/null +++ b/computer_oriented_statistical_methods/successive_approximation.cpp @@ -0,0 +1,27 @@ +#include +#include + +static float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +static float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } + +int main() { + float y, x1, x2, x3, sum, s, a, f1, f2, gd; + int i, n; + + for (i = 0; i < 10; i++) { + sum = eq(y); + std::cout << "value of equation at " << i << " " << sum << "\n"; + y++; + } + std::cout << "enter the x1->"; + std::cin >> x1; + std::cout << "enter the no iteration to perform->\n"; + std::cin >> n; + + for (i = 0; i <= n; i++) { + x2 = eqd(x1); + std::cout << "\nenter the x2->" << x2; + x1 = x2; + } + return 0; +} diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 4f6e0208197..0ffbcd40d78 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) { @@ -50,30 +56,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; } diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 1c07cd93fff..e82875da3c5 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -1,5 +1,5 @@ -#include #include +#include /* Calculate the the value on Fibonacci's sequence given an integer as input @@ -7,14 +7,13 @@ Fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... */ -int fibonacci(uint n) { +int fibonacci(unsigned int n) { /* If the input is 0 or 1 just return the same This will set the first 2 values of the sequence */ - if (n <= 1) - return n; + if (n <= 1) return n; /* Add the last 2 values of the sequence to get next */ - return fibonacci(n-1) + fibonacci(n-2); + return fibonacci(n - 1) + fibonacci(n - 2); } int main() { diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index 822cad332c6..c018de6662a 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -1,80 +1,67 @@ +#include +#include #include #include -#include -using namespace std; -// Declaring variables for maintaing prime numbers and to check whether a number is prime or not +// Declaring variables for maintaing prime numbers and to check whether a number +// is prime or not bool isprime[1000006]; -vector prime_numbers; -vector> factors; +std::vector prime_numbers; +std::vector> factors; // Calculating prime number upto a given range -void SieveOfEratosthenes(int N) -{ +void SieveOfEratosthenes(int N) { // initializes the array isprime memset(isprime, true, sizeof isprime); - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - { - for (int j = 2 * i; j <= N; j += i) - isprime[j] = false; + for (int i = 2; i <= N; i++) { + if (isprime[i]) { + for (int j = 2 * i; j <= N; j += i) isprime[j] = false; } } - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - prime_numbers.push_back(i); + for (int i = 2; i <= N; i++) { + if (isprime[i]) prime_numbers.push_back(i); } } // Prime factorization of a number -void prime_factorization(int num) -{ - +void prime_factorization(int num) { int number = num; - for (int i = 0; prime_numbers[i] <= num; i++) - { + for (int i = 0; prime_numbers[i] <= num; i++) { int count = 0; // termination condition - if (number == 1) - { + if (number == 1) { break; } - while (number % prime_numbers[i] == 0) - { + while (number % prime_numbers[i] == 0) { count++; number = number / prime_numbers[i]; } - if (count) - factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(std::make_pair(prime_numbers[i], count)); } } /* I added a simple UI. */ -int main() -{ +int main() { int num; - cout << "\t\tComputes the prime factorization\n\n"; - cout << "Type in a number: "; - cin >> num; + std::cout << "\t\tComputes the prime factorization\n\n"; + std::cout << "Type in a number: "; + std::cin >> num; SieveOfEratosthenes(num); prime_factorization(num); // Prime factors with their powers in the given number in new line - for (auto it : factors) - { - cout << it.first << " " << it.second << endl; + for (auto it : factors) { + std::cout << it.first << " " << it.second << std::endl; } return 0; diff --git a/math/primes_up_to_10^8.cpp b/math/primes_up_to_billion.cpp similarity index 79% rename from math/primes_up_to_10^8.cpp rename to math/primes_up_to_billion.cpp index db9b56cab32..d8f5a9f9d3e 100644 --- a/math/primes_up_to_10^8.cpp +++ b/math/primes_up_to_billion.cpp @@ -1,12 +1,12 @@ -#include #include +#include char prime[100000000]; void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index - prime[0] = '0'; // 0 is not prime - prime[1] = '0'; // 1 is not prime + prime[0] = '0'; // 0 is not prime + prime[1] = '0'; // 1 is not prime for (int p = 2; p * p <= n; p++) { if (prime[p] == '1') { for (int i = p * p; i <= n; i += p) @@ -15,7 +15,6 @@ void Sieve(int64_t n) { } } - int main() { Sieve(100000000); int64_t n; diff --git a/others/Buzz_number.cpp b/others/Buzz_number.cpp deleted file mode 100644 index f31038aad61..00000000000 --- a/others/Buzz_number.cpp +++ /dev/null @@ -1,17 +0,0 @@ -//A buzz number is a number that is either divisble 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; -} diff --git a/others/Decimal To Binary.cpp b/others/Decimal To Binary.cpp deleted file mode 100644 index 4e2119ebce8..00000000000 --- a/others/Decimal To Binary.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// This function convert decimal to binary number -// -#include -using namespace std; - -int main() -{ - int number; - cout << "Enter a number:"; - cin >> 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; -} diff --git a/others/Decimal To Hexadecimal .cpp b/others/Decimal To Hexadecimal .cpp deleted file mode 100644 index 705f21ba49f..00000000000 --- a/others/Decimal To Hexadecimal .cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -using namespace std; - -int main(void) -{ - int valueToConvert = 0; //Holds user input - int hexArray[8]; //Contains hex values backwards - int i = 0; //counter - char HexValues[] = "0123456789ABCDEF"; - - 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; - } - hexArray[i] = valueToConvert; //Gets last value - - cout << "Hex Value: "; - while (i >= 0) - cout << HexValues[hexArray[i--]]; - - cout << endl; - return 0; -} diff --git a/others/Decimal to Roman Numeral.cpp b/others/Decimal to Roman Numeral.cpp deleted file mode 100644 index 0372e800342..00000000000 --- a/others/Decimal to Roman Numeral.cpp +++ /dev/null @@ -1,97 +0,0 @@ -//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 -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; - 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); - 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); - 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 "?"; -} - -//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); - 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); - 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 "?"; -} - -//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"; - return 0; -} diff --git a/others/GCD_of_n_numbers.cpp b/others/GCD_of_n_numbers.cpp deleted file mode 100644 index 8158052f8fd..00000000000 --- a/others/GCD_of_n_numbers.cpp +++ /dev/null @@ -1,23 +0,0 @@ -//This program aims at calculating the GCD of n numbers by division method -#include -using namepsace std; -int main() -{ - cout << "Enter value of n:" << endl; - cin >> n; - int a[n]; - int i, j, gcd; - cout << "Enter the n numbers:" << endl; - for (i = 0; i < n; i++) - cin >> a[i]; - j = 1; //to access all elements of the array starting from 1 - gcd = a[0]; - while (j < n) - { - if (a[j] % gcd == 0) //value of gcd is as needed so far - j++; //so we check for next element - else - gcd = a[j] % gcd; //calculating GCD by division method - } - cout << "GCD of entered n numbers:" << gcd; -} diff --git a/others/Palindromeofnumber.cpp b/others/Palindromeofnumber.cpp deleted file mode 100644 index 64780399703..00000000000 --- a/others/Palindromeofnumber.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -using namespace std; - -int main() -{ - int num; - cout << "Enter number = "; - cin >> num; - - string s1 = to_string(num); - string s2 = s1; - - reverse(s1.begin(), s1.end()); - - if (s1 == s2) - cout << "true"; - else - cout << "false"; - - return 0; -} diff --git a/others/Sparse matrix.cpp b/others/Sparse matrix.cpp deleted file mode 100644 index 1861163f15f..00000000000 --- a/others/Sparse matrix.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/*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.*/ -#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"; - - // 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]; - } - } - - // 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"; -} diff --git a/others/Strassen Matrix Multiplication.cpp b/others/Strassen Matrix Multiplication.cpp deleted file mode 100644 index 85b62776385..00000000000 --- a/others/Strassen Matrix Multiplication.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -using namespace std; - -Multiply(int A[][], int B[][], int n) -{ - if (n == 2) - { - int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]); - int p2 = (a[1][0] + a[1][1]) * b[0][0]; - int p3 = a[0][0] * (b[0][1] - b[1][1]); - int p4 = a[1][1] * (b[1][0] - b[0][0]); - int p5 = (a[0][0] + a[0][1]) * b[1][1]; - int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]); - int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]); - - int c[n][n]; - c[0][0] = p1 + p4 - p5 + p7; - c[0][1] = p3 + p5; - c[1][0] = p2 + p4; - c[1][1] = p1 - p2 + p3 + p6; - - return c[][]; - } - else - { - } -} - -int main() -{ - int p, q, r, s; - cout << "Enter the dimensions of Matrices"; - cin >> n; - int A[n][n], ; - int B[n][n], ; - cout << "Enter the elements of Matrix A"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> A[i][j]; - } - } - - cout << "Enter the elements of Matrix B"; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - cin >> B[i][j]; - } - } - - Multiply(A, B, n); - return 0; -} \ No newline at end of file diff --git a/others/buzz_number.cpp b/others/buzz_number.cpp new file mode 100644 index 00000000000..ed9fc5f2888 --- /dev/null +++ b/others/buzz_number.cpp @@ -0,0 +1,20 @@ +/** + * @file + * @brief A buzz number is a number that is either divisible by 7 or has last + * digit as 7. + */ +#include + +/** 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; +} diff --git a/others/decimal_to_binary.cpp b/others/decimal_to_binary.cpp new file mode 100644 index 00000000000..11ce064a538 --- /dev/null +++ b/others/decimal_to_binary.cpp @@ -0,0 +1,55 @@ +/** + * @file + * @brief Function to convert decimal number to binary representation + */ +#include + +/** + * 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); + 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; +} diff --git a/others/decimal_to_hexadecimal.cpp b/others/decimal_to_hexadecimal.cpp new file mode 100644 index 00000000000..a3e544f49f8 --- /dev/null +++ b/others/decimal_to_hexadecimal.cpp @@ -0,0 +1,34 @@ +/** + * @file + * @brief Convert decimal number to hexadecimal representation + */ + +#include + +/** + * 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"; + + std::cout << "Enter a Decimal Value" + << std::endl; // Displays request to stdout + std::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 + + std::cout << "Hex Value: "; + while (i >= 0) std::cout << HexValues[hexArray[i--]]; + + std::cout << std::endl; + return 0; +} diff --git a/others/decimal_to_roman_numeral.cpp b/others/decimal_to_roman_numeral.cpp new file mode 100644 index 00000000000..6bd1be39511 --- /dev/null +++ b/others/decimal_to_roman_numeral.cpp @@ -0,0 +1,76 @@ +/** + * @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 + +/** 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 + */ +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 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 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 + */ +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 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 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() { + int 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; +} diff --git a/others/fibonacci.cpp b/others/fibonacci.cpp deleted file mode 100644 index 87ccda6d3dd..00000000000 --- a/others/fibonacci.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation -//This works by using both recursion and dynamic programming. -//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci -//we can use it for 10000th fibonacci etc, if we implement bigintegers. -//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci -//It is a property of fibonacci similar to matrix exponentiation. - -#include -#include -using namespace std; - -const long long MAX = 93; - -long long f[MAX] = {0}; - -long long fib(long long n) -{ - - if (n == 0) - return 0; - if (n == 1 || n == 2) - return (f[n] = 1); - - if (f[n]) - return f[n]; - - long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; - - f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) - : (2 * fib(k - 1) + fib(k)) * fib(k); - return f[n]; -} - -int main() -{ - //Main Function - for (long long i = 1; i < 93; i++) - { - cout << i << " th fibonacci number is " << fib(i) << "\n"; - } - return 0; -} diff --git a/others/fibonacci_fast.cpp b/others/fibonacci_fast.cpp new file mode 100644 index 00000000000..a0b83b640b2 --- /dev/null +++ b/others/fibonacci_fast.cpp @@ -0,0 +1,37 @@ +// An efficient way to calculate nth fibonacci number faster and simpler than +// O(nlogn) method of matrix exponentiation This works by using both recursion +// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be +// stored in a single long long variable, we can only use it till 92nd fibonacci +// we can use it for 10000th fibonacci etc, if we implement bigintegers. +// This algorithm works with the fact that nth fibonacci can easily found if we +// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci +// similar to matrix exponentiation. + +#include +#include +#include + +const uint64_t MAX = 93; + +uint64_t f[MAX] = {0}; + +uint64_t fib(uint64_t n) { + if (n == 0) return 0; + if (n == 1 || n == 2) return (f[n] = 1); + + if (f[n]) return f[n]; + + uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2; + + f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1)) + : (2 * fib(k - 1) + fib(k)) * fib(k); + return f[n]; +} + +int main() { + // Main Function + for (uint64_t i = 1; i < 93; i++) { + std::cout << i << " th fibonacci number is " << fib(i) << "\n"; + } + return 0; +} diff --git a/others/gcd_of_n_numbers.cpp b/others/gcd_of_n_numbers.cpp new file mode 100644 index 00000000000..37e82173a94 --- /dev/null +++ b/others/gcd_of_n_numbers.cpp @@ -0,0 +1,23 @@ +// This program aims at calculating the GCD of n numbers by division method +#include + +int main() { + int n; + std::cout << "Enter value of n:" << std::endl; + std::cin >> n; + int *a = new int[n]; + int i, j, gcd; + std::cout << "Enter the n numbers:" << std::endl; + for (i = 0; i < n; i++) std::cin >> a[i]; + j = 1; // to access all elements of the array starting from 1 + gcd = a[0]; + while (j < n) { + if (a[j] % gcd == 0) // value of gcd is as needed so far + j++; // so we check for next element + else + gcd = a[j] % gcd; // calculating GCD by division method + } + std::cout << "GCD of entered n numbers:" << gcd; + delete[] a; + return 0; +} diff --git a/others/matrix_exponentiation.cpp b/others/matrix_exponentiation.cpp index 25c411ddacb..f9b4997e970 100644 --- a/others/matrix_exponentiation.cpp +++ b/others/matrix_exponentiation.cpp @@ -19,6 +19,8 @@ The first element of this matrix is the required result. */ #include +#include + using std::cin; using std::cout; using std::vector; @@ -46,8 +48,7 @@ vector> multiply(vector> A, vector> B) { // computing power of a matrix vector> power(vector> A, ll p) { - if (p == 1) - return A; + if (p == 1) return A; if (p % 2 == 1) { return multiply(A, power(A, p - 1)); } else { @@ -58,14 +59,11 @@ vector> power(vector> A, ll p) { // main function ll ans(ll n) { - if (n == 0) - return 0; - if (n <= k) - return b[n - 1]; + if (n == 0) return 0; + if (n <= k) return b[n - 1]; // F1 vector F1(k + 1); - for (ll i = 1; i <= k; i++) - F1[i] = b[i - 1]; + for (ll i = 1; i <= k; i++) F1[i] = b[i - 1]; // Transpose matrix vector> T(k + 1, vector(k + 1)); diff --git a/others/measure_time_elapsed.cpp b/others/measure_time_elapsed.cpp deleted file mode 100644 index d0830ab79fe..00000000000 --- 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; -} diff --git a/others/palindrome_of_number.cpp b/others/palindrome_of_number.cpp new file mode 100644 index 00000000000..621b09ab6ba --- /dev/null +++ b/others/palindrome_of_number.cpp @@ -0,0 +1,27 @@ +#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 = "; + std::cin >> num; + + std::string s1 = std::to_string(num); + std::string s2 = s1; + + reverse(s1.begin(), s1.end()); + + if (s1 == s2) + std::cout << "true"; + else + std::cout << "false"; + + return 0; +} diff --git a/others/Paranthesis Matching.cpp b/others/paranthesis_matching.cpp similarity index 100% rename from others/Paranthesis Matching.cpp rename to others/paranthesis_matching.cpp diff --git a/others/pascal_triangle.cpp b/others/pascal_triangle.cpp index 101100018fa..063a6666a3a 100644 --- a/others/pascal_triangle.cpp +++ b/others/pascal_triangle.cpp @@ -1,63 +1,53 @@ -#include - -using namespace std; - -void show_pascal(int **arr, int n) -{ - //pint Pascal's Triangle - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n + i; ++j) - { - if (arr[i][j] == 0) - cout << " "; - else - cout << arr[i][j]; - } - cout << endl; - } +#include +#include + +void show_pascal(int **arr, int n) { + // pint Pascal's Triangle + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n + i; ++j) { + if (arr[i][j] == 0) + std::cout << " "; + else + std::cout << arr[i][j]; + } + std::cout << std::endl; + } } -int **pascal_triangle(int **arr, int n) -{ - for (int i = 0; i < n; ++i) - { - for (int j = n - i - 1; j < n + i; ++j) - { - if (j == n - i - 1 || j == n + i - 1) - arr[i][j] = 1; //The edge of the Pascal triangle goes in 1 - else - arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; - } - } - - return arr; +int **pascal_triangle(int **arr, int n) { + for (int i = 0; i < n; ++i) { + for (int j = n - i - 1; j < n + i; ++j) { + if (j == n - i - 1 || j == n + i - 1) + arr[i][j] = 1; // The edge of the Pascal triangle goes in 1 + else + arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1]; + } + } + + return arr; } -int main() -{ - int n = 0; - - cout << "Set Pascal's Triangle Height" << endl; - cin >> n; - - //memory allocation (Assign two-dimensional array to store Pascal triangle) - int **arr = new int*[n]; - for (int i = 0; i < n; ++i) - { - arr[i] = new int[2 * n - 1]; - memset(arr[i], 0, sizeof(int)*(2 * n - 1)); - } - - pascal_triangle(arr, n); - show_pascal(arr, n); - - //deallocation - for (int i = 0; i < n; ++i) - { - delete[] arr[i]; - } - delete[] arr; - - return 0; +int main() { + int n = 0; + + std::cout << "Set Pascal's Triangle Height" << std::endl; + std::cin >> n; + + // memory allocation (Assign two-dimensional array to store Pascal triangle) + int **arr = new int *[n]; + for (int i = 0; i < n; ++i) { + arr[i] = new int[2 * n - 1]; + memset(arr[i], 0, sizeof(int) * (2 * n - 1)); + } + + pascal_triangle(arr, n); + show_pascal(arr, n); + + // deallocation + for (int i = 0; i < n; ++i) { + delete[] arr[i]; + } + delete[] arr; + + return 0; } diff --git a/others/Primality Test.cpp b/others/primality_test.cpp similarity index 100% rename from others/Primality Test.cpp rename to others/primality_test.cpp diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp new file mode 100644 index 00000000000..d9878fda425 --- /dev/null +++ b/others/sparse_matrix.cpp @@ -0,0 +1,46 @@ +/** @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 + +int main() { + int m, n; + int counterZeros = 0; + + std::cout << "Enter dimensions of matrix (seperated with space): "; + std::cin >> m; + std::cin >> 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"; + + // 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 + } + } + + // 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"; + + for (int i = 0; i < m; i++) delete[] a[i]; + delete[] a; + return 0; +} diff --git a/others/String Fibonacci.cpp b/others/string_fibonacci.cpp similarity index 100% rename from others/String Fibonacci.cpp rename to others/string_fibonacci.cpp diff --git a/others/Tower of Hanoi.cpp b/others/tower_of_hanoi.cpp similarity index 100% rename from others/Tower of Hanoi.cpp rename to others/tower_of_hanoi.cpp diff --git a/sorting/BeadSort.cpp b/sorting/BeadSort.cpp deleted file mode 100644 index c8e27b2507e..00000000000 --- 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 6ad2489d229..00000000000 --- 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 b160ac4790a..00000000000 --- 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 cac6a361871..00000000000 --- 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/Insertion Sort.cpp b/sorting/Insertion Sort.cpp deleted file mode 100644 index 2563acaf8f2..00000000000 --- a/sorting/Insertion Sort.cpp +++ /dev/null @@ -1,40 +0,0 @@ -//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 : "; - - //Input - for (int i = 0; i < n; i++) - { - 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; - } - - //Output - cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) - { - cout << Array[i] << "\t"; - } - return 0; -} diff --git a/sorting/Merge Sort.cpp b/sorting/Merge Sort.cpp deleted file mode 100644 index b8edc8851f4..00000000000 --- a/sorting/Merge Sort.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -using namespace std; - -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]; - - 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]) - { - arr[k] = L[i]; - i++; - } - else - { - arr[k] = R[j]; - j++; - } - k++; - } - - while (i < n1) - { - arr[k] = L[i]; - i++; - k++; - } - - while (j < n2) - { - arr[k] = R[j]; - j++; - k++; - } -} - -void mergeSort(int arr[], int l, int r) -{ - if (l < r) - { - - int m = l + (r - l) / 2; - - mergeSort(arr, l, m); - mergeSort(arr, m + 1, r); - - merge(arr, l, m, r); - } -} - -void show(int A[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << A[i] << "\n"; -} - -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]; - } - - mergeSort(arr, 0, size); - - cout << "Sorted array\n"; - show(arr, size); - return 0; -} diff --git a/sorting/NumericStringSort.cpp b/sorting/NumericStringSort.cpp deleted file mode 100644 index 02f6964ba14..00000000000 --- 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/Radix Sort.cpp b/sorting/Radix Sort.cpp deleted file mode 100644 index 09c91bb2248..00000000000 --- a/sorting/Radix Sort.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#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]; - } - } -} -void print(int a[], int n) -{ - for (int i = 0; i < n; ++i) - { - cout << a[i] << " "; - } - cout << 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; -} \ No newline at end of file diff --git a/sorting/Shell Sort.cpp b/sorting/Shell Sort.cpp deleted file mode 100644 index b08c7ffd826..00000000000 --- a/sorting/Shell Sort.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#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]; - } - - // 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; -} diff --git a/sorting/Slow Sort.cpp b/sorting/Slow Sort.cpp deleted file mode 100644 index f8f7e74b804..00000000000 --- a/sorting/Slow Sort.cpp +++ /dev/null @@ -1,57 +0,0 @@ -//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); -} - -//Sample Main function - -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]; - } - - SlowSort(arr, 0, size); - - cout << "Sorted array\n"; - - for (int i = 0; i < size; ++i) - { - cout << arr[i] << " "; - } - return 0; -} diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp new file mode 100644 index 00000000000..a1a54c1e04a --- /dev/null +++ b/sorting/bead_sort.cpp @@ -0,0 +1,55 @@ +// 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 = new unsigned char[max * len]; + memset(beads, 0, max * len); + + // 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; + } + delete[] beads; +} + +// 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 00000000000..4d098105649 --- /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 00000000000..c43e425fcd9 --- /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; + bool swap_check = true; + 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); i++) { + swap_check = false; + for (int j = 0; j < n - 1 - i; j++) { + if (numbers[j] > numbers[j + 1]) { + swap_check = true; + 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/bucketSort.cpp deleted file mode 100644 index 0ffbf8e4cb7..00000000000 --- a/sorting/bucketSort.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// C++ program to sort an array using bucket sort -#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]; - - // 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()); - - // 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); - - cout << "Sorted array is \n"; - for (int i = 0; i < n; i++) - cout << arr[i] << " "; - return 0; -} diff --git a/sorting/bucket_sort.cpp b/sorting/bucket_sort.cpp new file mode 100644 index 00000000000..c43865281bc --- /dev/null +++ b/sorting/bucket_sort.cpp @@ -0,0 +1,36 @@ +// C++ program to sort an array using bucket sort +#include +#include +#include + +// Function to sort arr[] of size n using bucket sort +void bucketSort(float arr[], int n) { + // 1) Create n empty buckets + std::vector *b = new std::vector[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]); + } + + // 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]; + delete[] b; +} + +/* 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); + + std::cout << "Sorted array is \n"; + for (int i = 0; i < n; i++) std::cout << arr[i] << " "; + return 0; +} diff --git a/sorting/cocktail_selection_sort.cpp b/sorting/cocktail_selection_sort.cpp new file mode 100644 index 00000000000..ab622999687 --- /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/comb_sort.cpp b/sorting/comb_sort.cpp new file mode 100644 index 00000000000..1b0a4d70633 --- /dev/null +++ b/sorting/comb_sort.cpp @@ -0,0 +1,49 @@ +// 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 +#include + +int a[100005]; +int n; + +int FindNextGap(int x) { + x = (x * 10) / 13; + + return std::max(1, x); +} + +void CombSort(int a[], int l, int r) { + // Init gap + int gap = n; + + // 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 + 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]) { + std::swap(a[i], a[i + gap]); + swapped = true; + } + } + } +} + +int main() { + std::cin >> n; + for (int i = 1; i <= n; ++i) std::cin >> a[i]; + + CombSort(a, 1, n); + + for (int i = 1; i <= n; ++i) std::cout << a[i] << ' '; + return 0; +} diff --git a/sorting/combsort.cpp b/sorting/combsort.cpp deleted file mode 100644 index 2209c96fc55..00000000000 --- a/sorting/combsort.cpp +++ /dev/null @@ -1,59 +0,0 @@ -//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) -{ - x = (x * 10) / 13; - - return max(1, x); -} - -void CombSort(int a[], int l, int r) -{ - //Init gap - int gap = n; - - //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 - 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]); - swapped = true; - } - } - } -} - -int main() -{ - cin >> n; - for (int i = 1; i <= n; ++i) - cin >> a[i]; - - CombSort(a, 1, n); - - for (int i = 1; i <= n; ++i) - cout << a[i] << ' '; - return 0; -} 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/insertion_sort.cpp b/sorting/insertion_sort.cpp new file mode 100644 index 00000000000..fe920ca5913 --- /dev/null +++ b/sorting/insertion_sort.cpp @@ -0,0 +1,36 @@ +// Insertion Sort + +#include + +int main() { + int n; + std::cout << "\nEnter the length of your array : "; + std::cin >> n; + int *Array = new int[n]; + std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : "; + + // Input + for (int i = 0; i < n; i++) { + std::cin >> Array[i]; + } + + // Sorting + 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 + 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 new file mode 100644 index 00000000000..82ab869cd2c --- /dev/null +++ b/sorting/merge_sort.cpp @@ -0,0 +1,81 @@ +#include + +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 = 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]; + + i = 0; + j = 0; + k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } + + delete[] L; + delete[] R; +} + +void mergeSort(int arr[], int l, int r) { + if (l < r) { + int m = l + (r - l) / 2; + + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } +} + +void show(int A[], int size) { + int i; + for (i = 0; i < size; i++) std::cout << A[i] << "\n"; +} + +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::cout << "\n"; + std::cin >> arr[i]; + } + + mergeSort(arr, 0, size); + + std::cout << "Sorted array\n"; + show(arr, size); + + delete[] arr; + return 0; +} diff --git a/sorting/non_recursive_merge_sort.cpp b/sorting/non_recursive_merge_sort.cpp index c6ca56376df..ebfef1dcc50 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 00000000000..8b86bb29d51 --- /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 index d067fa068e8..53fa7339071 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -55,6 +55,7 @@ int partition(int arr[], int low, int high) { return (i + 1); } + /** * The main function that implements QuickSort * arr[] --> Array to be sorted, @@ -80,14 +81,21 @@ void show(int arr[], int size) { 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]; + std::cout << "\n"; + std::cin >> arr[i]; } - quickSort(arr, 0, size-1); - std::cout << "Sorted array : "; + quickSort(arr, 0, size); + std::cout << "Sorted array\n"; show(arr, size); + + delete[] arr; return 0; } diff --git a/sorting/radix_sort.cpp b/sorting/radix_sort.cpp new file mode 100644 index 00000000000..a0fbfe99ee5 --- /dev/null +++ b/sorting/radix_sort.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#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) { + 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; +} 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 new file mode 100644 index 00000000000..eb701478d4e --- /dev/null +++ b/sorting/shell_sort.cpp @@ -0,0 +1,37 @@ +#include + +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; + } + } + } + } + + // 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/shell_sort2.cpp b/sorting/shell_sort2.cpp new file mode 100644 index 00000000000..1268c7a50e7 --- /dev/null +++ b/sorting/shell_sort2.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +// for std::swap +#include + +template void show_data(T *arr, size_t LEN) { + size_t i; + + for (i = 0; i < LEN; i++) + std::cout << arr[i] << ", "; + std::cout << std::endl; +} + +template void show_data(T (&arr)[N]) { show_data(arr, N); } + +/** + * Optimized algorithm - takes half the time by utilizing + * Mar + **/ +template void shell_sort(T *arr, size_t LEN) { + const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; + const unsigned int gap_len = 8; + size_t i, j, g; + + for (g = 0; g < gap_len; g++) { + unsigned int gap = gaps[g]; + for (i = gap; i < LEN; i++) { + T tmp = arr[i]; + + for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) + arr[j] = arr[j - gap]; + + arr[j] = tmp; + } + } +} + +template void shell_sort(T (&arr)[N]) { + shell_sort(arr, N); +} + +/** + * function to compare sorting using cstdlib's qsort + **/ +int compare(const void *a, const void *b) { + int arg1 = *static_cast(a); + int arg2 = *static_cast(b); + + if (arg1 < arg2) + return -1; + if (arg1 > arg2) + return 1; + return 0; + + // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut + // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) +} + +int main(int argc, char *argv[]) { + int i, NUM_DATA; + + if (argc == 2) + NUM_DATA = atoi(argv[1]); + else + NUM_DATA = 200; + + // int array = new int[NUM_DATA]; + int *data = new int[NUM_DATA]; + int *data2 = new int[NUM_DATA]; + // int array2 = new int[NUM_DATA]; + int range = 1800; + + std::srand(time(NULL)); + for (i = 0; i < NUM_DATA; i++) + data[i] = data2[i] = (std::rand() % range) - (range >> 1); + + std::cout << "Unsorted original data: " << std::endl; + show_data(data, NUM_DATA); + std::clock_t start = std::clock(); + shell_sort(data, NUM_DATA); + std::clock_t end = std::clock(); + + std::cout << std::endl + << "Data Sorted using custom implementation: " << std::endl; + show_data(data, NUM_DATA); + + double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + start = std::clock(); + qsort(data2, NUM_DATA, sizeof(data2[0]), compare); + end = std::clock(); + std::cout << "Data Sorted using cstdlib qsort: " << std::endl; + show_data(data2, NUM_DATA); + + elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC; + std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl; + + free(data); + free(data2); + return 0; +} diff --git a/sorting/slow_sort.cpp b/sorting/slow_sort.cpp new file mode 100644 index 00000000000..b2627d12f3a --- /dev/null +++ b/sorting/slow_sort.cpp @@ -0,0 +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. + +#include + +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 + +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::cout << "\n"; + std::cin >> arr[i]; + } + + SlowSort(arr, 0, size); + + std::cout << "Sorted array\n"; + + for (int i = 0; i < size; ++i) { + std::cout << arr[i] << " "; + } + + delete[] arr; + return 0; +} diff --git a/sorting/swap_sort.cpp b/sorting/swap_sort.cpp index a4ab1e57b66..2e59e2fb3bf 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 similarity index 55% rename from sorting/Tim Sort.cpp rename to sorting/tim_sort.cpp index 14d3a04d049..94f5aa230b3 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;