From c6af94350830a38824fb69ca21b8b4bb111a1f49 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:38:54 +0530 Subject: [PATCH] fix: add cstdint header to all files using fixed width integers (#2717) * fix: add to subset_sum.cpp * fix: add to subarray_sum.cpp * fix: add to wildcard_matching.cpp * fix: add to count_bit_flips.cpp * fix: add to count_of_set_bits.cpp * fix: add to trailing_ciphers.cpp * fix: add to hamming_distance.cpp * doc: include doc for hamming_distance * fix: add to next_higher_numebr_with_same_number_of_set_bits.cpp * fix: add to power_of_2.cpp * fix: add to set_kth_bit.cpp * fix: add to bit_manipulation/set_kth_bit.cpp * fix: add to bit_manipulation/travelling_salesman_using_bit_manipulation.cpp * fix: add to ciphers/base64_encoding.cpp * fix: add to ciphers/hill_cipher.cpp * fix: add to ciphers/uint128_t.hpp * fix: add to data_structures/dsu_path_compression.cpp * fix: add to data_structures/dsu_path_compression.cpp * fix add to datastructures/list_array>cpp * fix add to datastructures/queue_using_array.cpp * fix: add to sparse_table.cpp * fix: add to stack_using_list_queue.cpp * fix: add to treap.cpp * fix: add to graham_scan_functions.hpp * fix: add to graph/** * fix: add integral typdefs to hashing/** * fix: add to math/** * fix: add to numerical_methods/** * fix: add to other/** * fix: add to search/** * fix: add to sorting/** * fix: add to string/** * doc: remove include statement from comment * fix: make tests static Co-authored-by: David Leal * fix: make tests static Co-authored-by: David Leal * chore: use iwyu on backtracking/**.cpp * chore: use iwyu on bit_manip/**.cpp * chore: use iwyu on ciphers/**.cpp * chore: use iwyu on cpu_scheduling_algorithms/**.cpp * chore: use iwyu on data_structures/**.cpp * chore: use iwyu on divide_and_conquer/**.cpp * chore: use iwyu on geometry/**.cpp * chore: use iwyu on graph/**.cpp * chore: use iwyu on hashing/**.cpp * chore: use iwyu on machine_learning/**.cpp * chore: use iwyu on math/**.cpp * chore: use iwyu on numerical_methods/**.cpp * chore: use iwyu on others/**.cpp * chore: use iwyu on probablity/**.cpp * chore: use iwyu on search/**.cpp * chore: use iwyu on sorting/**.cpp * chore: use iwyu on strings/**.cpp * Revert "chore: use iwyu on strings/**.cpp" This reverts commit f2127456a8eb3e25fc54f39e129578677838f6a4. * Revert "chore: use iwyu on sorting/**.cpp" This reverts commit a290ae7ee2bc73c32348aa74ef622794d02d4938. * Revert "chore: use iwyu on search/**.cpp" This reverts commit 19d136ae0fe13798b58b74a7932ce4c7a3950453. * Revert "chore: use iwyu on probablity/**.cpp" This reverts commit 5dd7f82a34501dc48b985c9fe4822cfa2ef125c2. * Revert "chore: use iwyu on others/**.cpp" This reverts commit 8a8fd42383207c24bee7dbf8f502ceb339b0e63d. * Revert "chore: use iwyu on numerical_methods/**.cpp" This reverts commit eff2f44a502aa437903938c6a9ebbc0f8ac7d278. * Revert "chore: use iwyu on math/**.cpp" This reverts commit c47117ca3f02aee4ba054b861bf7ab44e2ae6348. * Revert "chore: use iwyu on machine_learning/**.cpp" This reverts commit c3897d3763c5f0fb0077d761f824e754b7b181da. * Revert "chore: use iwyu on hashing/**.cpp" This reverts commit 0c6611a835d75718ae060b8b634ce47c85d7690b. * Revert "chore: use iwyu on graph/**.cpp" This reverts commit dabd6d2591d2feeefec2aa512fdcfd335e600606. * Revert "chore: use iwyu on geometry/**.cpp" This reverts commit 740bd65932f7db3e94b2f541b7eb3b9859be61c9. * Revert "chore: use iwyu on divide_and_conquer/**.cpp" This reverts commit 16ee49e0868d28d7c5fbefaf86d8e5a2b1c7cd7d. * Revert "chore: use iwyu on data_structures/**.cpp" This reverts commit a3b719e3684d32d966ed64f31d3c20bdfd3b5ea3. * Revert "chore: use iwyu on cpu_scheduling_algorithms/**.cpp" This reverts commit 24e597f7e249c7fd6998b2db39eba7a91d484455. * Revert "chore: use iwyu on ciphers/**.cpp" This reverts commit 3d80295883d15a6753f1e0e746c19a0c3ea35abe. * Revert "chore: use iwyu on bit_manip/**.cpp" This reverts commit 7edcb6e458fecd7ed05a7d4e87418680f443ec23. * Revert "chore: use iwyu on backtracking/**.cpp" This reverts commit f0a30d7cdbc0b06330b02d9ff0387feb3d9b380b. * Update search/binary_search.cpp * Update backtracking/subarray_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/wildcard_matching.cpp * Update bit_manipulation/count_bits_flip.cpp * Update bit_manipulation/count_of_set_bits.cpp * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp * Update bit_manipulation/hamming_distance.cpp * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp * Update bit_manipulation/power_of_2.cpp * Update others/lru_cache.cpp * Update bit_manipulation/set_kth_bit.cpp * Update bit_manipulation/travelling_salesman_using_bit_manipulation.cpp * Update ciphers/base64_encoding.cpp * Update ciphers/hill_cipher.cpp * Update ciphers/uint128_t.hpp * Update cpu_scheduling_algorithms/fcfs_scheduling.cpp * Update data_structures/dsu_path_compression.cpp * Update data_structures/dsu_union_rank.cpp * Update data_structures/list_array.cpp * Update data_structures/queue_using_array.cpp * Update data_structures/sparse_table.cpp * Update data_structures/stack_using_queue.cpp * Update data_structures/treap.cpp * Update geometry/graham_scan_functions.hpp * Update graph/bidirectional_dijkstra.cpp * Update graph/connected_components_with_dsu.cpp * Update graph/cycle_check_directed_graph.cpp * Update graph/is_graph_bipartite2.cpp * Update graph/travelling_salesman_problem.cpp * Update hashing/md5.cpp * Update hashing/sha1.cpp * Update math/n_choose_r.cpp * Update strings/z_function.cpp * Update strings/manacher_algorithm.cpp * Update sorting/wiggle_sort.cpp * Update sorting/selection_sort_recursive.cpp * Update sorting/selection_sort_iterative.cpp * Update sorting/recursive_bubble_sort.cpp * Update sorting/radix_sort2.cpp * Update sorting/dnf_sort.cpp * Update sorting/cycle_sort.cpp * Update search/sublist_search.cpp * Update search/saddleback_search.cpp * Update search/interpolation_search.cpp * Update search/floyd_cycle_detection_algo.cpp * Update search/exponential_search.cpp * Update search/exponential_search.cpp * Update math/n_bonacci.cpp * Update math/aliquot_sum.cpp * Update math/check_factorial.cpp * Update math/double_factorial.cpp * Update math/eulers_totient_function.cpp * Update math/factorial.cpp * Update math/fibonacci.cpp * Update math/fibonacci_matrix_exponentiation.cpp * Update math/fibonacci_sum.cpp * Update math/finding_number_of_digits_in_a_number.cpp * chore: remove "/// for integral typedefs" * chore: remove for integral typedefs from modular division * fix: remove comment from include * fix: add cstdint to gale shapely --------- Co-authored-by: David Leal --- backtracking/subarray_sum.cpp | 1 + backtracking/subset_sum.cpp | 1 + backtracking/wildcard_matching.cpp | 1 + bit_manipulation/count_bits_flip.cpp | 1 + bit_manipulation/count_of_set_bits.cpp | 1 + ...unt_of_trailing_ciphers_in_factorial_n.cpp | 1 + bit_manipulation/hamming_distance.cpp | 1 + ...er_number_with_same_number_of_set_bits.cpp | 1 + bit_manipulation/power_of_2.cpp | 1 + bit_manipulation/set_kth_bit.cpp | 1 + ...elling_salesman_using_bit_manipulation.cpp | 1 + ciphers/base64_encoding.cpp | 1 + ciphers/hill_cipher.cpp | 1 + ciphers/uint128_t.hpp | 1 + cpu_scheduling_algorithms/fcfs_scheduling.cpp | 1 + data_structures/dsu_path_compression.cpp | 1 + data_structures/dsu_union_rank.cpp | 1 + data_structures/list_array.cpp | 344 +++++++++--------- data_structures/queue_using_array.cpp | 1 + data_structures/sparse_table.cpp | 1 + data_structures/stack_using_queue.cpp | 1 + data_structures/treap.cpp | 7 +- dynamic_programming/trapped_rainwater.cpp | 4 +- geometry/graham_scan_functions.hpp | 1 + graph/bidirectional_dijkstra.cpp | 1 + graph/connected_components_with_dsu.cpp | 19 +- graph/cycle_check_directed_graph.cpp | 1 + graph/is_graph_bipartite2.cpp | 1 + graph/travelling_salesman_problem.cpp | 1 + greedy_algorithms/gale_shapley.cpp | 96 +++-- hashing/md5.cpp | 1 + hashing/sha1.cpp | 1 + math/aliquot_sum.cpp | 1 + math/check_factorial.cpp | 1 + math/double_factorial.cpp | 1 + math/eulers_totient_function.cpp | 14 +- math/factorial.cpp | 2 +- math/fibonacci.cpp | 2 + math/fibonacci_matrix_exponentiation.cpp | 163 +++++---- math/fibonacci_sum.cpp | 1 + math/finding_number_of_digits_in_a_number.cpp | 1 + math/integral_approximation.cpp | 35 +- math/inv_sqrt.cpp | 2 +- math/largest_power.cpp | 105 +++--- math/lcm_sum.cpp | 1 + math/linear_recurrence_matrix.cpp | 1 + math/magic_number.cpp | 1 + math/modular_division.cpp | 1 + math/modular_exponentiation.cpp | 2 +- math/modular_inverse_simple.cpp | 1 + math/n_bonacci.cpp | 2 +- math/n_choose_r.cpp | 2 +- math/sieve_of_eratosthenes.cpp | 2 +- math/string_fibonacci.cpp | 1 + math/sum_of_binomial_coefficient.cpp | 1 + numerical_methods/brent_method_extrema.cpp | 1 + numerical_methods/durand_kerner_roots.cpp | 1 + numerical_methods/fast_fourier_transform.cpp | 1 + numerical_methods/golden_search_extrema.cpp | 1 + .../inverse_fast_fourier_transform.cpp | 1 + numerical_methods/newton_raphson_method.cpp | 1 + numerical_methods/rungekutta.cpp | 2 +- others/easter.cpp | 1 + others/kadanes3.cpp | 2 +- others/lru_cache.cpp | 1 + search/binary_search.cpp | 1 + search/exponential_search.cpp | 2 +- search/floyd_cycle_detection_algo.cpp | 2 +- search/interpolation_search.cpp | 1 + search/saddleback_search.cpp | 51 +-- search/sublist_search.cpp | 1 + sorting/cycle_sort.cpp | 1 + sorting/dnf_sort.cpp | 1 + sorting/quick_sort.cpp | 1 + sorting/radix_sort2.cpp | 2 + sorting/recursive_bubble_sort.cpp | 51 +-- sorting/selection_sort_iterative.cpp | 1 + sorting/selection_sort_recursive.cpp | 7 +- sorting/wiggle_sort.cpp | 3 +- strings/manacher_algorithm.cpp | 1 + strings/z_function.cpp | 1 + 81 files changed, 559 insertions(+), 420 deletions(-) diff --git a/backtracking/subarray_sum.cpp b/backtracking/subarray_sum.cpp index 8001d74cc0d..ba9834e87a6 100644 --- a/backtracking/subarray_sum.cpp +++ b/backtracking/subarray_sum.cpp @@ -14,6 +14,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for unordered_map #include /// for std::vector diff --git a/backtracking/subset_sum.cpp b/backtracking/subset_sum.cpp index fad820920a5..8551a66e0ca 100644 --- a/backtracking/subset_sum.cpp +++ b/backtracking/subset_sum.cpp @@ -10,6 +10,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector diff --git a/backtracking/wildcard_matching.cpp b/backtracking/wildcard_matching.cpp index d9163a41b48..cd0bd72ad8c 100644 --- a/backtracking/wildcard_matching.cpp +++ b/backtracking/wildcard_matching.cpp @@ -12,6 +12,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index 5a6584b7241..2ab2ce31c15 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -20,6 +20,7 @@ * @author [Yash Raj Singh](https://github.com/yashrajyash) */ #include /// for assert +#include #include /// for IO operations /** * @namespace bit_manipulation diff --git a/bit_manipulation/count_of_set_bits.cpp b/bit_manipulation/count_of_set_bits.cpp index f2d802061cc..dd29fd8bf19 100644 --- a/bit_manipulation/count_of_set_bits.cpp +++ b/bit_manipulation/count_of_set_bits.cpp @@ -16,6 +16,7 @@ * @author [Prashant Thakur](https://github.com/prashant-th18) */ #include /// for assert +#include #include /// for IO operations /** * @namespace bit_manipulation diff --git a/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp b/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp index aa2ceb3dd8a..c1c57d0a617 100644 --- a/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp +++ b/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp @@ -18,6 +18,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/bit_manipulation/hamming_distance.cpp b/bit_manipulation/hamming_distance.cpp index 62879cf4883..ca4e9fc5b36 100644 --- a/bit_manipulation/hamming_distance.cpp +++ b/bit_manipulation/hamming_distance.cpp @@ -13,6 +13,7 @@ */ #include /// for assert +#include #include /// for io operations /** diff --git a/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp b/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp index d93884d4e11..f6d840f91bd 100644 --- a/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp +++ b/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp @@ -16,6 +16,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/bit_manipulation/power_of_2.cpp b/bit_manipulation/power_of_2.cpp index c62bfdaa3f5..f35361c8a9d 100644 --- a/bit_manipulation/power_of_2.cpp +++ b/bit_manipulation/power_of_2.cpp @@ -16,6 +16,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/bit_manipulation/set_kth_bit.cpp b/bit_manipulation/set_kth_bit.cpp index a58e663b163..d3d50e6cb28 100644 --- a/bit_manipulation/set_kth_bit.cpp +++ b/bit_manipulation/set_kth_bit.cpp @@ -19,6 +19,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp b/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp index 31243ed2921..9858b8107a3 100644 --- a/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp +++ b/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp @@ -22,6 +22,7 @@ */ #include /// for std::min #include /// for assert +#include #include /// for IO operations #include /// for limits of integral types #include /// for std::vector diff --git a/ciphers/base64_encoding.cpp b/ciphers/base64_encoding.cpp index a592226a5e7..81459408a8e 100644 --- a/ciphers/base64_encoding.cpp +++ b/ciphers/base64_encoding.cpp @@ -13,6 +13,7 @@ */ #include /// for `std::array` #include /// for `assert` operations +#include #include /// for IO operations /** diff --git a/ciphers/hill_cipher.cpp b/ciphers/hill_cipher.cpp index 1fef0f32e2d..d77a51c22b3 100644 --- a/ciphers/hill_cipher.cpp +++ b/ciphers/hill_cipher.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include #include diff --git a/ciphers/uint128_t.hpp b/ciphers/uint128_t.hpp index d50cb4ffb2d..7abffb08a61 100644 --- a/ciphers/uint128_t.hpp +++ b/ciphers/uint128_t.hpp @@ -8,6 +8,7 @@ */ #include /// for `std::reverse` and other operations +#include #include /// for `std::cout` overload #include /// for `std::string` #include /// for `std::pair` library diff --git a/cpu_scheduling_algorithms/fcfs_scheduling.cpp b/cpu_scheduling_algorithms/fcfs_scheduling.cpp index 4fc61af6bce..5c5e2fa3c58 100644 --- a/cpu_scheduling_algorithms/fcfs_scheduling.cpp +++ b/cpu_scheduling_algorithms/fcfs_scheduling.cpp @@ -11,6 +11,7 @@ #include /// for sorting #include /// for assert +#include #include /// random number generation #include /// for time #include /// for formatting the output diff --git a/data_structures/dsu_path_compression.cpp b/data_structures/dsu_path_compression.cpp index 022e632a784..c08040570dd 100644 --- a/data_structures/dsu_path_compression.cpp +++ b/data_structures/dsu_path_compression.cpp @@ -19,6 +19,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector diff --git a/data_structures/dsu_union_rank.cpp b/data_structures/dsu_union_rank.cpp index 8936d6d6964..16e1d3275bf 100644 --- a/data_structures/dsu_union_rank.cpp +++ b/data_structures/dsu_union_rank.cpp @@ -20,6 +20,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector diff --git a/data_structures/list_array.cpp b/data_structures/list_array.cpp index 05c66ed84fa..0c8099f5788 100644 --- a/data_structures/list_array.cpp +++ b/data_structures/list_array.cpp @@ -4,17 +4,20 @@ * * @details * The list_array is the implementation of list represented using array. - * We can perform basic CRUD operations as well as other operations like sorting etc. + * We can perform basic CRUD operations as well as other operations like sorting + * etc. * * ### Algorithm * It implements various method like insert, sort, search etc. efficiently. * You can select the operation and methods will do the rest work for you. - * You can insert element, sort them in order, search efficiently, delete values and print the list. + * You can insert element, sort them in order, search efficiently, delete values + * and print the list. */ -#include /// for io operations -#include /// for std::array -#include /// for assert +#include /// for std::array +#include /// for assert +#include +#include /// for io operations /** * @namespace data_structures @@ -23,181 +26,190 @@ namespace data_structures { /** * @namespace list_array - * @brief Functions for [Dynamic Array](https://en.wikipedia.org/wiki/Dynamic_array) algorithm + * @brief Functions for [Dynamic + * Array](https://en.wikipedia.org/wiki/Dynamic_array) algorithm */ namespace list_array { +/** + * @brief Structure of List with supporting methods. + */ +template +struct list { + std::array data{}; // Array that implement list + uint64_t top = 0; // Pointer to the last element + bool isSorted = false; // indicator whether list is sorted or not /** - * @brief Structure of List with supporting methods. + * @brief Search an element in the list using binarySearch. + * @param dataArr list + * @param first pointer to the first element in the remaining list + * @param last pointer to the last element in the remaining list + * @param val element that will be searched + * @return index of element in the list if present else -1 */ - template - struct list { - std::array data{}; // Array that implement list - uint64_t top = 0; // Pointer to the last element - bool isSorted = false; // indicator whether list is sorted or not - /** - * @brief Search an element in the list using binarySearch. - * @param dataArr list - * @param first pointer to the first element in the remaining list - * @param last pointer to the last element in the remaining list - * @param val element that will be searched - * @return index of element in the list if present else -1 - */ - uint64_t BinarySearch(const std::array &dataArr, const uint64_t &first, const uint64_t &last, - const uint64_t &val) { - // If both pointer cross each other means no element present in the list which is equal to the val - if (last < first) { - return -1; - } - uint64_t mid = (first + last) / 2; - // check whether current mid pointer value is equal to element or not - if (dataArr[mid] == val) - return mid; - // if current mid value is greater than element we have to search in first half - else if (val < dataArr[mid]) - return (BinarySearch(dataArr, first, mid - 1, val)); - // if current mid value is greater than element we have to search in second half - else if (val > dataArr[mid]) - return (BinarySearch(dataArr, mid + 1, last, val)); - - std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n"; + uint64_t BinarySearch(const std::array &dataArr, + const uint64_t &first, const uint64_t &last, + const uint64_t &val) { + // If both pointer cross each other means no element present in the list + // which is equal to the val + if (last < first) { return -1; } + uint64_t mid = (first + last) / 2; + // check whether current mid pointer value is equal to element or not + if (dataArr[mid] == val) + return mid; + // if current mid value is greater than element we have to search in + // first half + else if (val < dataArr[mid]) + return (BinarySearch(dataArr, first, mid - 1, val)); + // if current mid value is greater than element we have to search in + // second half + else if (val > dataArr[mid]) + return (BinarySearch(dataArr, mid + 1, last, val)); - /** - * @brief Search an element using linear search - * @param dataArr list - * @param val element that will be searched - * @return index of element in the list if present else -1 - */ - uint64_t LinearSearch(const std::array &dataArr, const uint64_t &val) const { - // Going through each element in the list - for (uint64_t i = 0; i < top; i++) { - if (dataArr[i] == val) { - return i; // element found at ith index - } + std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n"; + return -1; + } + + /** + * @brief Search an element using linear search + * @param dataArr list + * @param val element that will be searched + * @return index of element in the list if present else -1 + */ + uint64_t LinearSearch(const std::array &dataArr, + const uint64_t &val) const { + // Going through each element in the list + for (uint64_t i = 0; i < top; i++) { + if (dataArr[i] == val) { + return i; // element found at ith index } - // element is not present in the list - return -1; } + // element is not present in the list + return -1; + } - /* - * @brief Parent function of binarySearch and linearSearch methods - * @param val element that will be searched - * @return index of element in the list if present else -1 - */ - uint64_t search(const uint64_t &val) { - uint64_t pos; // pos variable to store index value of element. - // if list is sorted, binary search works efficiently else linear search is the only option - if (isSorted) { - pos = BinarySearch(data, 0, top - 1, val); - } else { - pos = LinearSearch(data, val); - } - // if index is equal to -1 means element does not present - // else print the index of that element - if (pos != -1) { - std::cout << "\nElement found at position : " << pos; - } else { - std::cout << "\nElement not found"; - } - // return the index of element or -1. - return pos; + /* + * @brief Parent function of binarySearch and linearSearch methods + * @param val element that will be searched + * @return index of element in the list if present else -1 + */ + uint64_t search(const uint64_t &val) { + uint64_t pos; // pos variable to store index value of element. + // if list is sorted, binary search works efficiently else linear search + // is the only option + if (isSorted) { + pos = BinarySearch(data, 0, top - 1, val); + } else { + pos = LinearSearch(data, val); + } + // if index is equal to -1 means element does not present + // else print the index of that element + if (pos != -1) { + std::cout << "\nElement found at position : " << pos; + } else { + std::cout << "\nElement not found"; } + // return the index of element or -1. + return pos; + } - /** - * @brief Sort the list - * @returns void - */ - void sort() { - //Going through each element in the list - for (uint64_t i = 0; i < top; i++) { - uint64_t min_idx = i; // Initialize the min variable - for (uint64_t j = i + 1; j < top; j++) { - // check whether any element less than current min value - if (data[j] < data[min_idx]) { - min_idx = j; // update index accordingly - } + /** + * @brief Sort the list + * @returns void + */ + void sort() { + // Going through each element in the list + for (uint64_t i = 0; i < top; i++) { + uint64_t min_idx = i; // Initialize the min variable + for (uint64_t j = i + 1; j < top; j++) { + // check whether any element less than current min value + if (data[j] < data[min_idx]) { + min_idx = j; // update index accordingly } - // swap min value and element at the ith index - std::swap(data[min_idx], data[i]); } - // mark isSorted variable as true - isSorted = true; + // swap min value and element at the ith index + std::swap(data[min_idx], data[i]); } + // mark isSorted variable as true + isSorted = true; + } - /** - * @brief Insert the new element in the list - * @param val element that will be inserted - * @returns void - */ - void insert(const uint64_t &val) { - // overflow check - if (top == N) { - std::cout << "\nOverflow"; - return; - } - // if list is not sorted, insert at the last - // otherwise place it to correct position - if (!isSorted) { - data[top] = val; - top++; - } else { - uint64_t pos = 0; // Initialize the index variable - // Going through each element and find correct position for element - for (uint64_t i = 0; i < top - 1; i++) { - // check for the correct position - if (data[i] <= val && val <= data[i + 1]) { - pos = i + 1; // assign correct pos to the index var - break; // to get out from the loop - } - } - // if all elements are smaller than the element - if (pos == 0) { - pos = top - 1; - } - // shift all element to make a room for new element - for (uint64_t i = top; i > pos; i--) { - data[i] = data[i - 1]; + /** + * @brief Insert the new element in the list + * @param val element that will be inserted + * @returns void + */ + void insert(const uint64_t &val) { + // overflow check + if (top == N) { + std::cout << "\nOverflow"; + return; + } + // if list is not sorted, insert at the last + // otherwise place it to correct position + if (!isSorted) { + data[top] = val; + top++; + } else { + uint64_t pos = 0; // Initialize the index variable + // Going through each element and find correct position for element + for (uint64_t i = 0; i < top - 1; i++) { + // check for the correct position + if (data[i] <= val && val <= data[i + 1]) { + pos = i + 1; // assign correct pos to the index var + break; // to get out from the loop } - top++; // Increment the value of top. - data[pos] = val; // Assign the value to the correct index in the array } - } - - /** - * @brief To remove the element from the list - * @param val element that will be removed - * @returns void - */ - void remove(const uint64_t &val) { - uint64_t pos = search(val); // search the index of the value - // if search returns -1, element does not present in the list - if (pos == -1) { - std::cout << "\n Element does not present in the list "; - return; + // if all elements are smaller than the element + if (pos == 0) { + pos = top - 1; } - std::cout << "\n" << data[pos] << " deleted"; // print the appropriate message - // shift all the element 1 left to fill vacant space - for (uint64_t i = pos; i < top; i++) { - data[i] = data[i + 1]; + // shift all element to make a room for new element + for (uint64_t i = top; i > pos; i--) { + data[i] = data[i - 1]; } - top--; // decrement the top variable to maintain last index + top++; // Increment the value of top. + data[pos] = + val; // Assign the value to the correct index in the array } + } - /** - * @brief Utility function to print array - * @returns void - */ - void show() { - // Going through each element in the list - std::cout << '\n'; - for (uint64_t i = 0; i < top; i++) { - std::cout << data[i] << " "; // print the element - } + /** + * @brief To remove the element from the list + * @param val element that will be removed + * @returns void + */ + void remove(const uint64_t &val) { + uint64_t pos = search(val); // search the index of the value + // if search returns -1, element does not present in the list + if (pos == -1) { + std::cout << "\n Element does not present in the list "; + return; + } + std::cout << "\n" + << data[pos] << " deleted"; // print the appropriate message + // shift all the element 1 left to fill vacant space + for (uint64_t i = pos; i < top; i++) { + data[i] = data[i + 1]; + } + top--; // decrement the top variable to maintain last index + } + + /** + * @brief Utility function to print array + * @returns void + */ + void show() { + // Going through each element in the list + std::cout << '\n'; + for (uint64_t i = 0; i < top; i++) { + std::cout << data[i] << " "; // print the element } - }; // structure list -} // namespace list_array -} // namespace data_structures + } +}; // structure list +} // namespace list_array +} // namespace data_structures /** * @brief Test implementations @@ -216,19 +228,19 @@ static void test() { L.insert(20); L.insert(18); assert(L.top == 7); - L.show(); // To print the array + L.show(); // To print the array // Remove testing - L.remove(12); // Remove Duplicate value in the list - L.remove(15); // Remove the existing value in the list + L.remove(12); // Remove Duplicate value in the list + L.remove(15); // Remove the existing value in the list assert(L.top == 5); - L.remove(50); // Try to remove the non-existing value in the list + L.remove(50); // Try to remove the non-existing value in the list assert(L.top == 5); // LinearSearch testing - assert(L.search(11) == 0); // search for the existing element + assert(L.search(11) == 0); // search for the existing element assert(L.search(12) == 2); - assert(L.search(50) == -1); // search for the non-existing element + assert(L.search(50) == -1); // search for the non-existing element // Sort testing L.sort(); @@ -236,9 +248,9 @@ static void test() { L.show(); // BinarySearch testing - assert(L.search(11) == 1); // search for the existing element + assert(L.search(11) == 1); // search for the existing element assert(L.search(12) == 2); - assert(L.search(50) == -1); // search for the non-existing element + assert(L.search(50) == -1); // search for the non-existing element } /** @@ -246,6 +258,6 @@ static void test() { * @returns 0 on exit */ int main() { - test(); // Execute the tests + test(); // Execute the tests return 0; } diff --git a/data_structures/queue_using_array.cpp b/data_structures/queue_using_array.cpp index 8b9b387c5b8..c2437258328 100644 --- a/data_structures/queue_using_array.cpp +++ b/data_structures/queue_using_array.cpp @@ -18,6 +18,7 @@ * @author [Farbod Ahmadian](https://github.com/farbodahm) */ #include /// for std::array +#include #include /// for io operations constexpr uint16_t max_size{10}; ///< Maximum size of the queue diff --git a/data_structures/sparse_table.cpp b/data_structures/sparse_table.cpp index b99616ae6b0..7b3d74b90c3 100644 --- a/data_structures/sparse_table.cpp +++ b/data_structures/sparse_table.cpp @@ -24,6 +24,7 @@ #include /// for std::array #include /// for assert +#include #include /// for IO operations /** diff --git a/data_structures/stack_using_queue.cpp b/data_structures/stack_using_queue.cpp index b849fcf053a..c5fc55a3e61 100644 --- a/data_structures/stack_using_queue.cpp +++ b/data_structures/stack_using_queue.cpp @@ -9,6 +9,7 @@ * @author [tushar2407](https://github.com/tushar2407) */ #include /// for assert +#include #include /// for IO operations #include /// for queue data structure diff --git a/data_structures/treap.cpp b/data_structures/treap.cpp index 522876941b1..592f49e7dfb 100644 --- a/data_structures/treap.cpp +++ b/data_structures/treap.cpp @@ -19,6 +19,7 @@ #include /// For array #include /// For assert +#include #include /// For IO operations /** @@ -157,11 +158,9 @@ struct Treap { } if (k == key[x]) { return size[childs[x][0]] + 1; - } - else if (k < key[x]) { + } else if (k < key[x]) { return _get_rank(childs[x][0], k); - } - else { + } else { return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k); } } diff --git a/dynamic_programming/trapped_rainwater.cpp b/dynamic_programming/trapped_rainwater.cpp index 8ac80835180..d9e16682754 100644 --- a/dynamic_programming/trapped_rainwater.cpp +++ b/dynamic_programming/trapped_rainwater.cpp @@ -11,8 +11,8 @@ #include /// For std::min and std::max #include /// For assert #include /// For std::size_t -#include /// For integral typedefs -#include /// For std::vector +#include +#include /// For std::vector /* * @namespace diff --git a/geometry/graham_scan_functions.hpp b/geometry/graham_scan_functions.hpp index f6e05095e74..58ac066e498 100644 --- a/geometry/graham_scan_functions.hpp +++ b/geometry/graham_scan_functions.hpp @@ -39,6 +39,7 @@ * *******************************************************************************/ #include /// for std::swap +#include #include /// for mathematics and datatype conversion #include /// for IO operations #include /// for std::stack diff --git a/graph/bidirectional_dijkstra.cpp b/graph/bidirectional_dijkstra.cpp index deef6622758..b4f58d22621 100644 --- a/graph/bidirectional_dijkstra.cpp +++ b/graph/bidirectional_dijkstra.cpp @@ -14,6 +14,7 @@ */ #include /// for assert +#include #include /// for io operations #include /// for variable INF #include /// for the priority_queue of distances diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp index c0194e85163..59dc2fda3cd 100644 --- a/graph/connected_components_with_dsu.cpp +++ b/graph/connected_components_with_dsu.cpp @@ -3,20 +3,24 @@ * @brief [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union) * * @details - * The Disjoint union is the technique to find connected component in graph efficiently. + * The Disjoint union is the technique to find connected component in graph + * efficiently. * * ### Algorithm - * In Graph, if you have to find out the number of connected components, there are 2 options + * In Graph, if you have to find out the number of connected components, there + * are 2 options * 1. Depth first search * 2. Disjoint union - * 1st option is inefficient, Disjoint union is the most optimal way to find this. + * 1st option is inefficient, Disjoint union is the most optimal way to find + * this. * * @author Unknown author * @author [Sagar Pandya](https://github.com/sagarpandyansit) */ -#include /// for IO operations -#include /// for std::set -#include /// for std::vector +#include +#include /// for IO operations +#include /// for std::set +#include /// for std::vector /** * @namespace graph @@ -25,7 +29,8 @@ namespace graph { /** * @namespace disjoint_union - * @brief Functions for [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union) implementation + * @brief Functions for [Disjoint + * union](https://en.wikipedia.org/wiki/Disjoint_union) implementation */ namespace disjoint_union { uint32_t number_of_nodes = 0; // denotes number of nodes diff --git a/graph/cycle_check_directed_graph.cpp b/graph/cycle_check_directed_graph.cpp index 8a651cfc5f6..15b92c4d6d2 100644 --- a/graph/cycle_check_directed_graph.cpp +++ b/graph/cycle_check_directed_graph.cpp @@ -7,6 +7,7 @@ * */ +#include #include // for std::cout #include // for std::map #include // for std::queue diff --git a/graph/is_graph_bipartite2.cpp b/graph/is_graph_bipartite2.cpp index f1b04d0707b..0711dfb9296 100644 --- a/graph/is_graph_bipartite2.cpp +++ b/graph/is_graph_bipartite2.cpp @@ -15,6 +15,7 @@ * @author [tushar2407](https://github.com/tushar2407) */ #include /// for assert +#include #include /// for IO operations #include /// for queue data structure #include /// for vector data structure diff --git a/graph/travelling_salesman_problem.cpp b/graph/travelling_salesman_problem.cpp index 28df3e93155..1b329d5cb32 100644 --- a/graph/travelling_salesman_problem.cpp +++ b/graph/travelling_salesman_problem.cpp @@ -19,6 +19,7 @@ #include /// for std::min #include /// for assert +#include #include /// for IO operations #include /// for limits of integral types #include /// for std::vector diff --git a/greedy_algorithms/gale_shapley.cpp b/greedy_algorithms/gale_shapley.cpp index ddc6298fa44..6267bf3b160 100644 --- a/greedy_algorithms/gale_shapley.cpp +++ b/greedy_algorithms/gale_shapley.cpp @@ -1,24 +1,26 @@ /** * @file - * @brief [Gale Shapley Algorithm](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm) + * @brief [Gale Shapley + * Algorithm](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm) * @details - * This implementation utilizes the Gale-Shapley algorithm to find stable matches. + * This implementation utilizes the Gale-Shapley algorithm to find stable + * matches. * - * **Gale Shapley Algorithm** aims to find a stable matching between two equally sized - * sets of elements given an ordinal preference for each element. The algorithm was - * introduced by David Gale and Lloyd Shapley in 1962. - * - * Reference: + * **Gale Shapley Algorithm** aims to find a stable matching between two equally + * sized sets of elements given an ordinal preference for each element. The + * algorithm was introduced by David Gale and Lloyd Shapley in 1962. + * + * Reference: * [Wikipedia](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm) * [Wikipedia](https://en.wikipedia.org/wiki/Stable_matching_problem) * * @author [B Karthik](https://github.com/BKarthik7) */ -#include /// for std::u32int_t -#include /// for std::vector #include /// for std::find #include /// for assert +#include /// for std::uint32_t +#include /// for std::vector /** * @namespace @@ -31,19 +33,25 @@ namespace greedy_algorithms { */ namespace stable_matching { /** - * @brief The main function that finds the stable matching between two sets of elements - * using the Gale-Shapley Algorithm. - * @note This doesn't work on negative preferences. the preferences should be continuous integers starting from - * 0 to number of preferences - 1. - * @param primary_preferences the preferences of the primary set should be a 2D vector - * @param secondary_preferences the preferences of the secondary set should be a 2D vector + * @brief The main function that finds the stable matching between two sets of + * elements using the Gale-Shapley Algorithm. + * @note This doesn't work on negative preferences. the preferences should be + * continuous integers starting from 0 to number of preferences - 1. + * @param primary_preferences the preferences of the primary set should be a 2D + * vector + * @param secondary_preferences the preferences of the secondary set should be a + * 2D vector * @returns matches the stable matching between the two sets */ -std::vector gale_shapley(const std::vector>& secondary_preferences, const std::vector>& primary_preferences) { +std::vector gale_shapley( + const std::vector>& secondary_preferences, + const std::vector>& primary_preferences) { std::uint32_t num_elements = secondary_preferences.size(); std::vector matches(num_elements, -1); std::vector is_free_primary(num_elements, true); - std::vector proposal_index(num_elements, 0); // Tracks the next secondary to propose for each primary + std::vector proposal_index( + num_elements, + 0); // Tracks the next secondary to propose for each primary while (true) { int free_primary_index = -1; @@ -57,10 +65,13 @@ std::vector gale_shapley(const std::vector gale_shapley(const std::vector gale_shapley(const std::vector> primary_preferences = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}}; - std::vector> secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}}; - assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector({0, 2, 1, 3})); + std::vector> primary_preferences = { + {0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}}; + std::vector> secondary_preferences = { + {1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}}; + assert(greedy_algorithms::stable_matching::gale_shapley( + secondary_preferences, primary_preferences) == + std::vector({0, 2, 1, 3})); // Test Case 2 - primary_preferences = {{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}}; - secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}}; - assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector({0, 3, 1, 2})); + primary_preferences = { + {0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}}; + secondary_preferences = { + {1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}}; + assert(greedy_algorithms::stable_matching::gale_shapley( + secondary_preferences, primary_preferences) == + std::vector({0, 3, 1, 2})); // Test Case 3 primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}}; secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}}; - assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector({0, 2, 1})); + assert(greedy_algorithms::stable_matching::gale_shapley( + secondary_preferences, primary_preferences) == + std::vector({0, 2, 1})); // Test Case 4 primary_preferences = {}; secondary_preferences = {}; - assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector({})); + assert(greedy_algorithms::stable_matching::gale_shapley( + secondary_preferences, primary_preferences) == + std::vector({})); } /** @@ -124,6 +150,6 @@ static void tests() { * @returns 0 on exit */ int main() { - tests(); // Run self-test implementations + tests(); // Run self-test implementations return 0; } diff --git a/hashing/md5.cpp b/hashing/md5.cpp index aad75731007..52bc898f7d4 100644 --- a/hashing/md5.cpp +++ b/hashing/md5.cpp @@ -41,6 +41,7 @@ #include /// Used for std::copy #include /// Used for std::array #include /// Used for assert +#include #include /// Used for std::memcopy #include /// Used for IO operations #include /// Used for strings diff --git a/hashing/sha1.cpp b/hashing/sha1.cpp index 106da827296..2d551b99316 100644 --- a/hashing/sha1.cpp +++ b/hashing/sha1.cpp @@ -32,6 +32,7 @@ #include /// For std::copy #include /// For std::array #include /// For assert +#include #include /// For std::memcopy #include /// For IO operations #include /// For strings diff --git a/math/aliquot_sum.cpp b/math/aliquot_sum.cpp index 6d94d53a1be..0b8afdf8920 100644 --- a/math/aliquot_sum.cpp +++ b/math/aliquot_sum.cpp @@ -20,6 +20,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/math/check_factorial.cpp b/math/check_factorial.cpp index 0be45b89509..0b63433ebe4 100644 --- a/math/check_factorial.cpp +++ b/math/check_factorial.cpp @@ -10,6 +10,7 @@ * @author [ewd00010](https://github.com/ewd00010) */ #include /// for assert +#include #include /// for cout /** diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 72feda60c4e..5832ce440d6 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -10,6 +10,7 @@ */ #include +#include #include /** Compute double factorial using iterative method diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index f1752e9e92a..768034327da 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,6 +1,7 @@ /** * @file - * @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) + * @brief Implementation of [Euler's + * Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) * @description * Euler Totient Function is also known as phi function. * \f[\phi(n) = @@ -24,8 +25,9 @@ * @author [Mann Mehta](https://github.com/mann2108) */ -#include /// for IO operations -#include /// for assert +#include /// for assert +#include +#include /// for IO operations /** * @brief Mathematical algorithms @@ -39,12 +41,14 @@ namespace math { uint64_t phiFunction(uint64_t n) { uint64_t result = n; for (uint64_t i = 2; i * i <= n; i++) { - if (n % i != 0) continue; + if (n % i != 0) + continue; while (n % i == 0) n /= i; result -= result / i; } - if (n > 1) result -= result / n; + if (n > 1) + result -= result / n; return result; } diff --git a/math/factorial.cpp b/math/factorial.cpp index acfa053d89a..aca1886c783 100644 --- a/math/factorial.cpp +++ b/math/factorial.cpp @@ -12,8 +12,8 @@ */ #include /// for assert +#include #include /// for I/O operations - /** * @namespace * @brief Mathematical algorithms diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index a09ebb57822..79048040ac4 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -9,6 +9,8 @@ * * @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp */ + +#include #include /// for assert #include /// for IO operations diff --git a/math/fibonacci_matrix_exponentiation.cpp b/math/fibonacci_matrix_exponentiation.cpp index 1a119d210aa..915cf62b580 100644 --- a/math/fibonacci_matrix_exponentiation.cpp +++ b/math/fibonacci_matrix_exponentiation.cpp @@ -1,101 +1,103 @@ /** - * @file + * @file * @brief This program computes the N^th Fibonacci number in modulo mod * input argument . * * Takes O(logn) time to compute nth Fibonacci number - * + * * * \author [villayatali123](https://github.com/villayatali123) * \author [unknown author]() - * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp + * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, + * fibonacci_large.cpp */ -#include -#include #include +#include +#include +#include /** * This function finds nth fibonacci number in a given modulus * @param n nth fibonacci number - * @param mod modulo number + * @param mod modulo number */ -uint64_t fibo(uint64_t n , uint64_t mod ) -{ - std::vector result(2,0); - std::vector> transition(2,std::vector(2,0)); - std::vector> Identity(2,std::vector(2,0)); - n--; - result[0]=1, result[1]=1; - Identity[0][0]=1; Identity[0][1]=0; - Identity[1][0]=0; Identity[1][1]=1; - - transition[0][0]=0; - transition[1][0]=transition[1][1]=transition[0][1]=1; - - while(n) - { - if(n%2) - { - std::vector> res(2, std::vector(2,0)); - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - for(int k=0;k<2;k++) - { - res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod; - } - } - } - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - Identity[i][j]=res[i][j]; - } - } - n--; - } - else{ - std::vector> res1(2, std::vector(2,0)); - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - for(int k=0;k<2;k++) - { - res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod; - } - } - } - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - transition[i][j]=res1[i][j]; - } - } - n=n/2; - } - } - return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod; +uint64_t fibo(uint64_t n, uint64_t mod) { + std::vector result(2, 0); + std::vector> transition(2, + std::vector(2, 0)); + std::vector> Identity(2, std::vector(2, 0)); + n--; + result[0] = 1, result[1] = 1; + Identity[0][0] = 1; + Identity[0][1] = 0; + Identity[1][0] = 0; + Identity[1][1] = 1; + + transition[0][0] = 0; + transition[1][0] = transition[1][1] = transition[0][1] = 1; + + while (n) { + if (n % 2) { + std::vector> res(2, + std::vector(2, 0)); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + res[i][j] = + (res[i][j] % mod + + ((Identity[i][k] % mod * transition[k][j] % mod)) % + mod) % + mod; + } + } + } + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + Identity[i][j] = res[i][j]; + } + } + n--; + } else { + std::vector> res1( + 2, std::vector(2, 0)); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + res1[i][j] = + (res1[i][j] % mod + ((transition[i][k] % mod * + transition[k][j] % mod)) % + mod) % + mod; + } + } + } + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + transition[i][j] = res1[i][j]; + } + } + n = n / 2; + } + } + return ((result[0] % mod * Identity[0][0] % mod) % mod + + (result[1] % mod * Identity[1][0] % mod) % mod) % + mod; } /** * Function to test above algorithm */ -void test() -{ - assert(fibo(6, 1000000007 ) == 8); +static void test() { + assert(fibo(6, 1000000007) == 8); std::cout << "test case:1 passed\n"; - assert(fibo(5, 1000000007 ) == 5); + assert(fibo(5, 1000000007) == 5); std::cout << "test case:2 passed\n"; - assert(fibo(10 , 1000000007) == 55); + assert(fibo(10, 1000000007) == 55); std::cout << "test case:3 passed\n"; - assert(fibo(500 , 100) == 25); + assert(fibo(500, 100) == 25); std::cout << "test case:3 passed\n"; - assert(fibo(500 , 10000) == 4125); + assert(fibo(500, 10000) == 4125); std::cout << "test case:3 passed\n"; std::cout << "--All tests passed--\n"; } @@ -103,11 +105,12 @@ void test() /** * Main function */ -int main() -{ - test(); - uint64_t mod=1000000007; - std::cout<<"Enter the value of N: "; - uint64_t n=0; std::cin>>n; - std::cout<> n; + std::cout << n << "th Fibonacci number in modulo " << mod << ": " + << fibo(n, mod) << std::endl; } diff --git a/math/fibonacci_sum.cpp b/math/fibonacci_sum.cpp index 5107578888e..9373e21cb87 100644 --- a/math/fibonacci_sum.cpp +++ b/math/fibonacci_sum.cpp @@ -13,6 +13,7 @@ */ #include /// for assert +#include #include /// for std::cin and std::cout #include /// for std::vector diff --git a/math/finding_number_of_digits_in_a_number.cpp b/math/finding_number_of_digits_in_a_number.cpp index 32ddd649ec3..b0edb746d65 100644 --- a/math/finding_number_of_digits_in_a_number.cpp +++ b/math/finding_number_of_digits_in_a_number.cpp @@ -18,6 +18,7 @@ #include /// for assert #include /// for log calculation +#include #include /// for IO operations /** diff --git a/math/integral_approximation.cpp b/math/integral_approximation.cpp index 7e72ef4042b..2d1d42f916d 100644 --- a/math/integral_approximation.cpp +++ b/math/integral_approximation.cpp @@ -1,18 +1,29 @@ /** * @file - * @brief Compute integral approximation of the function using [Riemann sum](https://en.wikipedia.org/wiki/Riemann_sum) - * @details In mathematics, a Riemann sum is a certain kind of approximation of an integral by a finite sum. It is named after nineteenth-century German mathematician Bernhard Riemann. - * One very common application is approximating the area of functions or lines on a graph and the length of curves and other approximations. - * The sum is calculated by partitioning the region into shapes (rectangles, trapezoids, parabolas, or cubics) that form a region similar to the region being measured, then calculating the area for each of these shapes, and finally adding all of these small areas together. - * This approach can be used to find a numerical approximation for a definite integral even if the fundamental theorem of calculus does not make it easy to find a closed-form solution. - * Because the region filled by the small shapes is usually not the same shape as the region being measured, the Riemann sum will differ from the area being measured. - * This error can be reduced by dividing up the region more finely, using smaller and smaller shapes. As the shapes get smaller and smaller, the sum approaches the Riemann integral. - * \author [Benjamin Walton](https://github.com/bwalton24) - * \author [Shiqi Sheng](https://github.com/shiqisheng00) + * @brief Compute integral approximation of the function using [Riemann + * sum](https://en.wikipedia.org/wiki/Riemann_sum) + * @details In mathematics, a Riemann sum is a certain kind of approximation of + * an integral by a finite sum. It is named after nineteenth-century German + * mathematician Bernhard Riemann. One very common application is approximating + * the area of functions or lines on a graph and the length of curves and other + * approximations. The sum is calculated by partitioning the region into shapes + * (rectangles, trapezoids, parabolas, or cubics) that form a region similar to + * the region being measured, then calculating the area for each of these + * shapes, and finally adding all of these small areas together. This approach + * can be used to find a numerical approximation for a definite integral even if + * the fundamental theorem of calculus does not make it easy to find a + * closed-form solution. Because the region filled by the small shapes is + * usually not the same shape as the region being measured, the Riemann sum will + * differ from the area being measured. This error can be reduced by dividing up + * the region more finely, using smaller and smaller shapes. As the shapes get + * smaller and smaller, the sum approaches the Riemann integral. \author + * [Benjamin Walton](https://github.com/bwalton24) \author [Shiqi + * Sheng](https://github.com/shiqisheng00) */ -#include /// for assert -#include /// for mathematical functions -#include /// for passing in functions +#include /// for assert +#include /// for mathematical functions +#include +#include /// for passing in functions #include /// for IO operations /** diff --git a/math/inv_sqrt.cpp b/math/inv_sqrt.cpp index ef490ddf918..96a13fc08a1 100644 --- a/math/inv_sqrt.cpp +++ b/math/inv_sqrt.cpp @@ -10,9 +10,9 @@ #include /// for assert #include /// for `std::sqrt` +#include #include /// for IO operations #include /// for numeric_limits - /** * @brief This is the function that calculates the fast inverse square root. * The following code is the fast inverse square root implementation from diff --git a/math/largest_power.cpp b/math/largest_power.cpp index 8fffbbd078b..bc1ba484754 100644 --- a/math/largest_power.cpp +++ b/math/largest_power.cpp @@ -1,41 +1,42 @@ /** * @file - * @brief Algorithm to find largest x such that p^x divides n! (factorial) using Legendre's Formula. - * @details Given an integer n and a prime number p, the task is to find the largest x such that - * p^x (p raised to power x) divides n! (factorial). This will be done using Legendre's formula: - * x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)] + \ldots + 1 - * @see more on https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n + * @brief Algorithm to find largest x such that p^x divides n! (factorial) using + * Legendre's Formula. + * @details Given an integer n and a prime number p, the task is to find the + * largest x such that p^x (p raised to power x) divides n! (factorial). This + * will be done using Legendre's formula: x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)] + * + \ldots + 1 + * @see more on + * https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n * @author [uday6670](https://github.com/uday6670) */ -#include /// for std::cin and std::cout -#include /// for assert - +#include /// for assert +#include +#include /// for std::cin and std::cout /** * @namespace math * @brief Mathematical algorithms */ namespace math { - /** - * @brief Function to calculate largest power - * @param n number - * @param p prime number - * @returns largest power - */ - uint64_t largestPower(uint32_t n, const uint16_t& p) - { - // Initialize result - int x = 0; - - // Calculate result - while (n) - { - n /= p; - x += n; - } - return x; +/** + * @brief Function to calculate largest power + * @param n number + * @param p prime number + * @returns largest power + */ +uint64_t largestPower(uint32_t n, const uint16_t& p) { + // Initialize result + int x = 0; + + // Calculate result + while (n) { + n /= p; + x += n; } + return x; +} } // namespace math @@ -43,36 +44,34 @@ namespace math { * @brief Function for testing largestPower function. * test cases and assert statement. * @returns `void` -*/ -static void test() -{ - uint8_t test_case_1 = math::largestPower(5,2); - assert(test_case_1==3); - std::cout<<"Test 1 Passed!"< /// for assert +#include #include /// for std::cin and std::cout #include /// for std::vector diff --git a/math/linear_recurrence_matrix.cpp b/math/linear_recurrence_matrix.cpp index 98c90e5f8fa..c54d1272bf7 100644 --- a/math/linear_recurrence_matrix.cpp +++ b/math/linear_recurrence_matrix.cpp @@ -18,6 +18,7 @@ * @author [Ashish Daulatabad](https://github.com/AshishYUO) */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector STL diff --git a/math/magic_number.cpp b/math/magic_number.cpp index 51232601b2b..c07869e3dfe 100644 --- a/math/magic_number.cpp +++ b/math/magic_number.cpp @@ -17,6 +17,7 @@ * @author [Neha Hasija](https://github.com/neha-hasija17) */ #include /// for assert +#include #include /// for io operations /** diff --git a/math/modular_division.cpp b/math/modular_division.cpp index 6fd984f2bf0..d37341b4a47 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -25,6 +25,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/math/modular_exponentiation.cpp b/math/modular_exponentiation.cpp index 01410860fda..d4a9dd84d07 100644 --- a/math/modular_exponentiation.cpp +++ b/math/modular_exponentiation.cpp @@ -17,8 +17,8 @@ * @author [Shri2206](https://github.com/Shri2206) */ #include /// for assert +#include #include /// for io operations - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/modular_inverse_simple.cpp b/math/modular_inverse_simple.cpp index 813f0e0b4a5..f8a36d4eae9 100644 --- a/math/modular_inverse_simple.cpp +++ b/math/modular_inverse_simple.cpp @@ -8,6 +8,7 @@ */ #include /// for assert +#include #include /// for IO operations /** diff --git a/math/n_bonacci.cpp b/math/n_bonacci.cpp index c34dab7a19d..bb2e85d13e4 100644 --- a/math/n_bonacci.cpp +++ b/math/n_bonacci.cpp @@ -16,9 +16,9 @@ */ #include /// for assert +#include #include /// for std::cout #include /// for std::vector - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/n_choose_r.cpp b/math/n_choose_r.cpp index 8bb3bbcdc2d..5e151f39a84 100644 --- a/math/n_choose_r.cpp +++ b/math/n_choose_r.cpp @@ -11,8 +11,8 @@ */ #include /// for assert +#include #include /// for io operations - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/sieve_of_eratosthenes.cpp b/math/sieve_of_eratosthenes.cpp index e003706d12b..29115d306d5 100644 --- a/math/sieve_of_eratosthenes.cpp +++ b/math/sieve_of_eratosthenes.cpp @@ -12,6 +12,7 @@ * @see primes_up_to_billion.cpp prime_numbers.cpp */ +#include #include /// for assert #include /// for IO operations #include /// for std::vector @@ -109,7 +110,6 @@ static void tests() { false}; assert(is_prime_3 == expected_3); - std::cout << "All tests have passed successfully!\n"; } /** diff --git a/math/string_fibonacci.cpp b/math/string_fibonacci.cpp index eb9b6d7e1a7..1aef80f7a3c 100644 --- a/math/string_fibonacci.cpp +++ b/math/string_fibonacci.cpp @@ -8,6 +8,7 @@ * @see fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp */ +#include #include #ifdef _MSC_VER #include // use this for MS Visual C diff --git a/math/sum_of_binomial_coefficient.cpp b/math/sum_of_binomial_coefficient.cpp index 1942df527dd..c5b1fbeb681 100644 --- a/math/sum_of_binomial_coefficient.cpp +++ b/math/sum_of_binomial_coefficient.cpp @@ -10,6 +10,7 @@ * @author [muskan0719](https://github.com/muskan0719) */ #include /// for assert +#include #include /// for std::cin and std::cout /** diff --git a/numerical_methods/brent_method_extrema.cpp b/numerical_methods/brent_method_extrema.cpp index 654a694514a..ea3c783bc53 100644 --- a/numerical_methods/brent_method_extrema.cpp +++ b/numerical_methods/brent_method_extrema.cpp @@ -16,6 +16,7 @@ #define _USE_MATH_DEFINES ///< required for MS Visual C++ #include #include +#include #include #include #include diff --git a/numerical_methods/durand_kerner_roots.cpp b/numerical_methods/durand_kerner_roots.cpp index 9bf0619b829..a5afc6a0046 100644 --- a/numerical_methods/durand_kerner_roots.cpp +++ b/numerical_methods/durand_kerner_roots.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/numerical_methods/fast_fourier_transform.cpp b/numerical_methods/fast_fourier_transform.cpp index 23a6c8a1f54..392789ea710 100644 --- a/numerical_methods/fast_fourier_transform.cpp +++ b/numerical_methods/fast_fourier_transform.cpp @@ -22,6 +22,7 @@ #include /// for assert #include /// for mathematical-related functions #include /// for storing points and coefficents +#include #include /// for IO operations #include /// for std::vector diff --git a/numerical_methods/golden_search_extrema.cpp b/numerical_methods/golden_search_extrema.cpp index 1ca47949fee..3f72db06f56 100644 --- a/numerical_methods/golden_search_extrema.cpp +++ b/numerical_methods/golden_search_extrema.cpp @@ -10,6 +10,7 @@ #define _USE_MATH_DEFINES //< required for MS Visual C++ #include #include +#include #include #include #include diff --git a/numerical_methods/inverse_fast_fourier_transform.cpp b/numerical_methods/inverse_fast_fourier_transform.cpp index 3837c21f966..928a20da802 100644 --- a/numerical_methods/inverse_fast_fourier_transform.cpp +++ b/numerical_methods/inverse_fast_fourier_transform.cpp @@ -14,6 +14,7 @@ #include /// for assert #include /// for mathematical-related functions #include /// for storing points and coefficents +#include #include /// for IO operations #include /// for std::vector diff --git a/numerical_methods/newton_raphson_method.cpp b/numerical_methods/newton_raphson_method.cpp index 17147e0bedd..1f5c9849f74 100644 --- a/numerical_methods/newton_raphson_method.cpp +++ b/numerical_methods/newton_raphson_method.cpp @@ -13,6 +13,7 @@ * \see bisection_method.cpp, false_position.cpp */ #include +#include #include #include #include diff --git a/numerical_methods/rungekutta.cpp b/numerical_methods/rungekutta.cpp index 01ca3a0745a..8522bd0ebc2 100644 --- a/numerical_methods/rungekutta.cpp +++ b/numerical_methods/rungekutta.cpp @@ -19,9 +19,9 @@ * There can be many such equations */ #include /// asserting the test functions +#include #include /// for io operations #include /// for using the vector container - /** * @brief The change() function is used * to return the updated iterative value corresponding diff --git a/others/easter.cpp b/others/easter.cpp index 36ac48028fc..6ae357deba7 100644 --- a/others/easter.cpp +++ b/others/easter.cpp @@ -18,6 +18,7 @@ */ #include /// for assert +#include #include /// for IO operations /* diff --git a/others/kadanes3.cpp b/others/kadanes3.cpp index 9cc6604e9c5..3d7ef442cfd 100644 --- a/others/kadanes3.cpp +++ b/others/kadanes3.cpp @@ -17,8 +17,8 @@ #include /// for std::array #include /// for assert #include /// for INT_MIN value +#include #include /// for IO operations - /** * @brief Utility function to check the current maximum number * \param arr input array diff --git a/others/lru_cache.cpp b/others/lru_cache.cpp index f9cd3caec7d..29f8bd0ed16 100644 --- a/others/lru_cache.cpp +++ b/others/lru_cache.cpp @@ -46,6 +46,7 @@ * */ #include /// for assert +#include #include /// for IO Operations #include /// for std::list #include /// for std::unordered_map diff --git a/search/binary_search.cpp b/search/binary_search.cpp index bed938b290c..ebe488e5658 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -37,6 +37,7 @@ #include /// for std::sort function #include /// for std::assert +#include #include /// for IO operations #include /// for std::vector /****************************************************************************** diff --git a/search/exponential_search.cpp b/search/exponential_search.cpp index f57cbf96b51..6443084dc25 100644 --- a/search/exponential_search.cpp +++ b/search/exponential_search.cpp @@ -14,7 +14,7 @@ */ #include #include -#include +#include #ifdef _MSC_VER #include // use for MS Visual C++ #else diff --git a/search/floyd_cycle_detection_algo.cpp b/search/floyd_cycle_detection_algo.cpp index c7dd95aea8c..58c994dccf3 100644 --- a/search/floyd_cycle_detection_algo.cpp +++ b/search/floyd_cycle_detection_algo.cpp @@ -12,9 +12,9 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector - /** * @namespace search * @brief Search algorithms diff --git a/search/interpolation_search.cpp b/search/interpolation_search.cpp index 5ed75774959..234d90ff698 100644 --- a/search/interpolation_search.cpp +++ b/search/interpolation_search.cpp @@ -31,6 +31,7 @@ #include /// for std::sort function #include /// for std::assert +#include #include /// for IO operations #include /// for std::vector diff --git a/search/saddleback_search.cpp b/search/saddleback_search.cpp index dab5adcf059..d75c65e3ea6 100644 --- a/search/saddleback_search.cpp +++ b/search/saddleback_search.cpp @@ -1,28 +1,34 @@ /** * @file - * @brief Implementation of [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array) for 2D arrays. + * @brief Implementation of [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array) + * for 2D arrays. * * @details * Saddleback Algorithm is an algorithm that searches 2D array in linear time, - * i.e, O(m + n), where m is number of rows and n is number of columns of 2D array. Also, each row and - * column of the matrix should be sorted beforehand for this algorithm to work. + * i.e, O(m + n), where m is number of rows and n is number of columns of 2D + * array. Also, each row and column of the matrix should be sorted beforehand + * for this algorithm to work. * * @author [Hashir Niazi](https://github.com/HashirGJ8842) */ -#include /// for assert -#include /// for io operations, and std::pair -#include /// for std::vector +#include /// for assert +#include +#include /// for io operations, and std::pair +#include /// for std::vector /** \namespace search * \brief Algorithms for searching */ namespace search { /** \namespace saddleback - * \brief Function for implementing [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array). + * \brief Function for implementing [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array). */ namespace saddleback { /** - * This function implements [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array), + * This function implements [Saddleback + * Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array), * on a sorted 2D array, and finds the location of the element needed to search * @param matrix 2D matrix which is sorted on the basis of rows and columns * @param element element to be searched @@ -30,16 +36,17 @@ namespace saddleback { * element is present. * @return An std::pair with (0, 0), if the element is not present. */ -std::pair saddleback(std::vector> matrix, - int32_t element) { +std::pair saddleback( + std::vector> matrix, int32_t element) { uint32_t left_index = 0; uint32_t right_index = matrix[0].size() - 1; // Start from top right corner - while (left_index < matrix.size()) { // Exit once the value of indexes get out of range. + while (left_index < + matrix.size()) { // Exit once the value of indexes get out of range. if (element == matrix[left_index] [right_index]) { // If value on this position of matrix is // equal to element, return (row, column). - return std::make_pair(left_index+1, right_index+1); + return std::make_pair(left_index + 1, right_index + 1); } else if (element > matrix[left_index] [right_index]) { // Else if value on this position of @@ -51,14 +58,15 @@ std::pair saddleback(std::vector> matri [right_index]) { // Else if value on this position of // matrix is greater than the // element, move down. - if(!right_index) + if (!right_index) break; - else --right_index; + else + --right_index; } } return std::make_pair( 0, 0); // If the program reaches here, that means one of the index - // went out of index, hence no element present. + // went out of index, hence no element present. } } // namespace saddleback } // namespace search @@ -69,15 +77,16 @@ std::pair saddleback(std::vector> matri */ static void test() { std::vector> matrix = {{1, 10, 100, 1000, 10000}, - {2, 20, 200, 2000, 20000}, - {3, 30, 300, 3000, 30000}, - {4, 40, 400, 4000, 40000}, - {5, 50, 500, 5000, 50000}}; + {2, 20, 200, 2000, 20000}, + {3, 30, 300, 3000, 30000}, + {4, 40, 400, 4000, 40000}, + {5, 50, 500, 5000, 50000}}; std::pair not_found = std::make_pair(0, 0); std::pair test_answer; // Test 1 - std::pair answer1 = search::saddleback::saddleback(matrix, 123); + std::pair answer1 = + search::saddleback::saddleback(matrix, 123); assert(not_found == answer1); // Test 2 answer1 = search::saddleback::saddleback(matrix, 0); @@ -101,6 +110,6 @@ static void test() { * @returns 0 on exit */ int main() { - test(); // execute the tests + test(); // execute the tests return 0; } diff --git a/search/sublist_search.cpp b/search/sublist_search.cpp index 0954173d20c..bb63cac0fa5 100644 --- a/search/sublist_search.cpp +++ b/search/sublist_search.cpp @@ -26,6 +26,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector diff --git a/sorting/cycle_sort.cpp b/sorting/cycle_sort.cpp index 8acf462af78..88364bf12a0 100644 --- a/sorting/cycle_sort.cpp +++ b/sorting/cycle_sort.cpp @@ -13,6 +13,7 @@ #include /// for std::is_sorted, std::swap #include /// for assert +#include #include /// for io operations #include /// for std::vector diff --git a/sorting/dnf_sort.cpp b/sorting/dnf_sort.cpp index 8b7c2279e0e..4fe0129cc81 100644 --- a/sorting/dnf_sort.cpp +++ b/sorting/dnf_sort.cpp @@ -12,6 +12,7 @@ #include /// for std::is_sorted #include /// for assert +#include #include /// for std::swap and io operations #include /// for std::vector diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 514361444e4..8a582790817 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -26,6 +26,7 @@ #include /// for std::is_sorted #include /// for std::assert +#include #include /// for std::time #include /// for IO operations #include /// for std::vector diff --git a/sorting/radix_sort2.cpp b/sorting/radix_sort2.cpp index 3d6916e1617..d20e81bccae 100644 --- a/sorting/radix_sort2.cpp +++ b/sorting/radix_sort2.cpp @@ -23,8 +23,10 @@ */ /// header files + #include /// for collection of functions #include /// for a macro called assert which can be used to verify assumptions +#include #include /// for io operations #include /// for std::vector diff --git a/sorting/recursive_bubble_sort.cpp b/sorting/recursive_bubble_sort.cpp index d76a72d8633..f73cc0aeac5 100644 --- a/sorting/recursive_bubble_sort.cpp +++ b/sorting/recursive_bubble_sort.cpp @@ -1,20 +1,23 @@ /** * @file * @author [Aditya Prakash](https://adityaprakash.tech) - * @brief This is an implementation of a recursive version of the [Bubble sort algorithm](https://www.geeksforgeeks.org/recursive-bubble-sort/) + * @brief This is an implementation of a recursive version of the [Bubble sort + algorithm](https://www.geeksforgeeks.org/recursive-bubble-sort/) * * @details * The working principle of the Bubble sort algorithm. - * Bubble sort is a simple sorting algorithm used to rearrange a set of ascending or descending order elements. - * Bubble sort gets its name from the fact that data "bubbles" to the top of the dataset. - + * Bubble sort is a simple sorting algorithm used to rearrange a set of + ascending or descending order elements. + * Bubble sort gets its name from the fact that data "bubbles" to the top of the + dataset. + * ### Algorithm * What is Swap? * Swapping two numbers means that we interchange their values. - * Often, an additional variable is required for this operation. + * Often, an additional variable is required for this operation. * This is further illustrated in the following: * void swap(int x, int y){ @@ -26,15 +29,19 @@ * The above process is a typical displacement process. * When we assign a value to x, the old value of x is lost. * That's why we create a temporary variable z to store the initial value of x. - * z is further used to assign the initial value of x to y, to complete swapping. + * z is further used to assign the initial value of x to y, to complete + swapping. * Recursion - * While the recursive method does not necessarily have advantages over iterative + * While the recursive method does not necessarily have advantages over + iterative * versions, but it is useful to enhance the understanding of the algorithm and * recursion itself. In Recursive Bubble sort algorithm, we firstly call the - * function on the entire array, and for every subsequent function call, we exclude - * the last element. This fixes the last element for that sub-array.Formally, for + * function on the entire array, and for every subsequent function call, we + exclude + * the last element. This fixes the last element for that sub-array.Formally, + for * `ith` iteration, we consider elements up to n-i, where n is the number of * elements in the array. Exit condition: n==1; i.e. the sub-array contains only * one element. @@ -43,16 +50,19 @@ * Time complexity: O(n) best case; O(n²) average case; O(n²) worst case * Space complexity: O(n) - * We need to traverse the array `n * (n-1)` times. However, if the entire array is - * already sorted, then we need to traverse it only once. Hence, O(n) is the best case + * We need to traverse the array `n * (n-1)` times. However, if the entire array + is + * already sorted, then we need to traverse it only once. Hence, O(n) is the + best case * complexity */ -#include /// for assert -#include /// for IO operations -#include /// for std::vector -#include /// for std::array -#include /// for std::is_sorted +#include /// for std::is_sorted +#include /// for std::array +#include /// for assert +#include +#include /// for IO operations +#include /// for std::vector /** * @namespace sorting @@ -61,11 +71,11 @@ namespace sorting { /** - * @brief This is an implementation of the recursive_bubble_sort. A vector is passed - * to the function which is then dereferenced, so that the changes are + * @brief This is an implementation of the recursive_bubble_sort. A vector is + * passed to the function which is then dereferenced, so that the changes are * reflected in the original vector. It also accepts a second parameter of * type `int` and name `n`, which is the size of the array. - * + * * @tparam T type of data variables in the array * @param nums our array of elements. * @param n size of the array @@ -136,14 +146,13 @@ static void test() { std::cout << double_arr[i] << ", "; } std::cout << std::endl; - } /** * @brief Main function * @returns 0 on exit */ -int main() { +int main() { test(); // run self-test implementations return 0; } diff --git a/sorting/selection_sort_iterative.cpp b/sorting/selection_sort_iterative.cpp index a9adac0891b..db293b88d03 100644 --- a/sorting/selection_sort_iterative.cpp +++ b/sorting/selection_sort_iterative.cpp @@ -29,6 +29,7 @@ *******************************************************************************/ #include /// for std::is_sorted #include /// for std::assert +#include #include /// for IO operations #include /// for std::vector diff --git a/sorting/selection_sort_recursive.cpp b/sorting/selection_sort_recursive.cpp index 57eadfbd0fe..2ef6219b8dc 100644 --- a/sorting/selection_sort_recursive.cpp +++ b/sorting/selection_sort_recursive.cpp @@ -28,9 +28,10 @@ */ #include /// for std::is_sorted -#include /// for assert -#include /// for std::swap and io operations -#include /// for std::vector +#include /// for assert +#include +#include /// for std::swap and io operations +#include /// for std::vector /** * @namespace sorting diff --git a/sorting/wiggle_sort.cpp b/sorting/wiggle_sort.cpp index bf8574c98c5..45ba788a86e 100644 --- a/sorting/wiggle_sort.cpp +++ b/sorting/wiggle_sort.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include /// for io operations #include @@ -50,7 +51,7 @@ namespace wiggle_sort { */ template // this allows to have vectors of ints, double, float, // etc - std::vector wiggleSort(const std::vector &arr) { +std::vector wiggleSort(const std::vector &arr) { uint32_t size = arr.size(); std::vector out( diff --git a/strings/manacher_algorithm.cpp b/strings/manacher_algorithm.cpp index 94314c3b466..e544d818d8c 100644 --- a/strings/manacher_algorithm.cpp +++ b/strings/manacher_algorithm.cpp @@ -11,6 +11,7 @@ */ #include /// for assert +#include #include /// for IO operations #include /// for std::vector STL #ifdef _MSC_VER diff --git a/strings/z_function.cpp b/strings/z_function.cpp index 6ce98c491d1..d32f19b9f4a 100644 --- a/strings/z_function.cpp +++ b/strings/z_function.cpp @@ -11,6 +11,7 @@ * @author [Ritika Gupta](https://github.com/RitikaGupta8734) */ +#include #include /// for IO operations #ifdef _MSC_VER #include /// for string (use this for MS Visual C++)