mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 02:25:57 +08:00
fix: add cstdint header to all files using fixed width integers (#2717)
* fix: add <cstdint> to subset_sum.cpp * fix: add <cstdint> to subarray_sum.cpp * fix: add <cstdint> to wildcard_matching.cpp * fix: add <cstdint> to count_bit_flips.cpp * fix: add <cstdint> to count_of_set_bits.cpp * fix: add <cstdint> to trailing_ciphers.cpp * fix: add <cstdint> to hamming_distance.cpp * doc: include doc for hamming_distance * fix: add <cstdint> to next_higher_numebr_with_same_number_of_set_bits.cpp * fix: add <cstdint> to power_of_2.cpp * fix: add <cstdint> to set_kth_bit.cpp * fix: add <cstdint> to bit_manipulation/set_kth_bit.cpp * fix: add <cstdint> to bit_manipulation/travelling_salesman_using_bit_manipulation.cpp * fix: add <cstdint> to ciphers/base64_encoding.cpp * fix: add <cstdint> to ciphers/hill_cipher.cpp * fix: add <cstdint> to ciphers/uint128_t.hpp * fix: add <cstdint> to data_structures/dsu_path_compression.cpp * fix: add <cstdint> to data_structures/dsu_path_compression.cpp * fix add <cstdint> to datastructures/list_array>cpp * fix add <cstdint> to datastructures/queue_using_array.cpp * fix: add <cstdint> to sparse_table.cpp * fix: add <cstdint> to stack_using_list_queue.cpp * fix: add <cstdint> to treap.cpp * fix: add <cstdint> to graham_scan_functions.hpp * fix: add <cstdint> to graph/** * fix: add integral typdefs to hashing/** * fix: add <cstdint> to math/** * fix: add <cstdint> to numerical_methods/** * fix: add <cstdint> to other/** * fix: add <cstdint> to search/** * fix: add <cstdint> to sorting/** * fix: add <cstdint> to string/** * doc: remove include statement from comment * fix: make tests static Co-authored-by: David Leal <halfpacho@gmail.com> * fix: make tests static Co-authored-by: David Leal <halfpacho@gmail.com> * 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 commitf2127456a8. * Revert "chore: use iwyu on sorting/**.cpp" This reverts commita290ae7ee2. * Revert "chore: use iwyu on search/**.cpp" This reverts commit19d136ae0f. * Revert "chore: use iwyu on probablity/**.cpp" This reverts commit5dd7f82a34. * Revert "chore: use iwyu on others/**.cpp" This reverts commit8a8fd42383. * Revert "chore: use iwyu on numerical_methods/**.cpp" This reverts commiteff2f44a50. * Revert "chore: use iwyu on math/**.cpp" This reverts commitc47117ca3f. * Revert "chore: use iwyu on machine_learning/**.cpp" This reverts commitc3897d3763. * Revert "chore: use iwyu on hashing/**.cpp" This reverts commit0c6611a835. * Revert "chore: use iwyu on graph/**.cpp" This reverts commitdabd6d2591. * Revert "chore: use iwyu on geometry/**.cpp" This reverts commit740bd65932. * Revert "chore: use iwyu on divide_and_conquer/**.cpp" This reverts commit16ee49e086. * Revert "chore: use iwyu on data_structures/**.cpp" This reverts commita3b719e368. * Revert "chore: use iwyu on cpu_scheduling_algorithms/**.cpp" This reverts commit24e597f7e2. * Revert "chore: use iwyu on ciphers/**.cpp" This reverts commit3d80295883. * Revert "chore: use iwyu on bit_manip/**.cpp" This reverts commit7edcb6e458. * Revert "chore: use iwyu on backtracking/**.cpp" This reverts commitf0a30d7cdb. * 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 <halfpacho@gmail.com>
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <unordered_map> /// for unordered_map
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
* @author [Yash Raj Singh](https://github.com/yashrajyash)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
/**
|
||||
* @namespace bit_manipulation
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* @author [Prashant Thakur](https://github.com/prashant-th18)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
/**
|
||||
* @namespace bit_manipulation
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
#include <algorithm> /// for std::min
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <limits> /// for limits of integral types
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
#include <array> /// for `std::array`
|
||||
#include <cassert> /// for `assert` operations
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <algorithm> /// for `std::reverse` and other operations
|
||||
#include <cstdint>
|
||||
#include <ostream> /// for `std::cout` overload
|
||||
#include <string> /// for `std::string`
|
||||
#include <utility> /// for `std::pair` library
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <algorithm> /// for sorting
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <cstdlib> /// random number generation
|
||||
#include <ctime> /// for time
|
||||
#include <iomanip> /// for formatting the output
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -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 <iostream> /// for io operations
|
||||
#include <array> /// for std::array
|
||||
#include <cassert> /// for assert
|
||||
#include <array> /// for std::array
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// 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 <uint64_t N>
|
||||
struct list {
|
||||
std::array<uint64_t, N> 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 <uint64_t N>
|
||||
struct list {
|
||||
std::array<uint64_t, N> 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<uint64_t, N> &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<uint64_t, N> &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<uint64_t, N> &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<uint64_t, N> &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;
|
||||
}
|
||||
|
||||
/*
|
||||
* @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
|
||||
}
|
||||
}
|
||||
// element is not present in the list
|
||||
return -1;
|
||||
// swap min value and element at the ith index
|
||||
std::swap(data[min_idx], data[i]);
|
||||
}
|
||||
// mark isSorted variable as true
|
||||
isSorted = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* @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 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
}
|
||||
// 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
|
||||
}
|
||||
// swap min value and element at the ith index
|
||||
std::swap(data[min_idx], data[i]);
|
||||
}
|
||||
// mark isSorted variable as true
|
||||
isSorted = true;
|
||||
// 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];
|
||||
}
|
||||
top++; // Increment the value of top.
|
||||
data[pos] =
|
||||
val; // Assign the value to the correct index in the array
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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];
|
||||
}
|
||||
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;
|
||||
}
|
||||
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 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
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
* @author [Farbod Ahmadian](https://github.com/farbodahm)
|
||||
*/
|
||||
#include <array> /// for std::array
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
constexpr uint16_t max_size{10}; ///< Maximum size of the queue
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#include <array> /// for std::array
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* @author [tushar2407](https://github.com/tushar2407)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <queue> /// for queue data structure
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <array> /// For array
|
||||
#include <cassert> /// For assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
#include <algorithm> /// For std::min and std::max
|
||||
#include <cassert> /// For assert
|
||||
#include <cstddef> /// For std::size_t
|
||||
#include <cstdint> /// For integral typedefs
|
||||
#include <vector> /// For std::vector
|
||||
#include <cstdint>
|
||||
#include <vector> /// For std::vector
|
||||
|
||||
/*
|
||||
* @namespace
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
*
|
||||
*******************************************************************************/
|
||||
#include <algorithm> /// for std::swap
|
||||
#include <cstdint>
|
||||
#include <cstdlib> /// for mathematics and datatype conversion
|
||||
#include <iostream> /// for IO operations
|
||||
#include <stack> /// for std::stack
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
#include <limits> /// for variable INF
|
||||
#include <queue> /// for the priority_queue of distances
|
||||
|
||||
@@ -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 <iostream> /// for IO operations
|
||||
#include <set> /// for std::set
|
||||
#include <vector> /// for std::vector
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <set> /// for std::set
|
||||
#include <vector> /// 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
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream> // for std::cout
|
||||
#include <map> // for std::map
|
||||
#include <queue> // for std::queue
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
* @author [tushar2407](https://github.com/tushar2407)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <queue> /// for queue data structure
|
||||
#include <vector> /// for vector data structure
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <algorithm> /// for std::min
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <limits> /// for limits of integral types
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
@@ -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 <iostream> /// for std::u32int_t
|
||||
#include <vector> /// for std::vector
|
||||
#include <algorithm> /// for std::find
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for std::uint32_t
|
||||
#include <vector> /// 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<std::uint32_t> gale_shapley(const std::vector<std::vector<std::uint32_t>>& secondary_preferences, const std::vector<std::vector<std::uint32_t>>& primary_preferences) {
|
||||
std::vector<std::uint32_t> gale_shapley(
|
||||
const std::vector<std::vector<std::uint32_t>>& secondary_preferences,
|
||||
const std::vector<std::vector<std::uint32_t>>& primary_preferences) {
|
||||
std::uint32_t num_elements = secondary_preferences.size();
|
||||
std::vector<std::uint32_t> matches(num_elements, -1);
|
||||
std::vector<bool> is_free_primary(num_elements, true);
|
||||
std::vector<std::uint32_t> proposal_index(num_elements, 0); // Tracks the next secondary to propose for each primary
|
||||
std::vector<std::uint32_t> 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<std::uint32_t> gale_shapley(const std::vector<std::vector<std::uint3
|
||||
}
|
||||
|
||||
// If no free primary is found, break the loop
|
||||
if (free_primary_index == -1) break;
|
||||
if (free_primary_index == -1)
|
||||
break;
|
||||
|
||||
// Get the next secondary to propose
|
||||
std::uint32_t secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
|
||||
std::uint32_t secondary_to_propose =
|
||||
primary_preferences[free_primary_index]
|
||||
[proposal_index[free_primary_index]];
|
||||
proposal_index[free_primary_index]++;
|
||||
|
||||
// Get the current match of the secondary
|
||||
@@ -72,18 +83,21 @@ std::vector<std::uint32_t> gale_shapley(const std::vector<std::vector<std::uint3
|
||||
is_free_primary[free_primary_index] = false;
|
||||
} else {
|
||||
// Determine if the current match should be replaced
|
||||
auto new_proposer_rank = std::find(secondary_preferences[secondary_to_propose].begin(),
|
||||
secondary_preferences[secondary_to_propose].end(),
|
||||
free_primary_index);
|
||||
auto current_match_rank = std::find(secondary_preferences[secondary_to_propose].begin(),
|
||||
secondary_preferences[secondary_to_propose].end(),
|
||||
current_match);
|
||||
auto new_proposer_rank =
|
||||
std::find(secondary_preferences[secondary_to_propose].begin(),
|
||||
secondary_preferences[secondary_to_propose].end(),
|
||||
free_primary_index);
|
||||
auto current_match_rank =
|
||||
std::find(secondary_preferences[secondary_to_propose].begin(),
|
||||
secondary_preferences[secondary_to_propose].end(),
|
||||
current_match);
|
||||
|
||||
// If the new proposer is preferred over the current match
|
||||
if (new_proposer_rank < current_match_rank) {
|
||||
matches[secondary_to_propose] = free_primary_index;
|
||||
is_free_primary[free_primary_index] = false;
|
||||
is_free_primary[current_match] = true; // Current match is now free
|
||||
is_free_primary[current_match] =
|
||||
true; // Current match is now free
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,24 +113,36 @@ std::vector<std::uint32_t> gale_shapley(const std::vector<std::vector<std::uint3
|
||||
*/
|
||||
static void tests() {
|
||||
// Test Case 1
|
||||
std::vector<std::vector<std::uint32_t>> primary_preferences = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
|
||||
std::vector<std::vector<std::uint32_t>> 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<std::uint32_t>({0, 2, 1, 3}));
|
||||
std::vector<std::vector<std::uint32_t>> primary_preferences = {
|
||||
{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
|
||||
std::vector<std::vector<std::uint32_t>> 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<std::uint32_t>({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<std::uint32_t>({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<std::uint32_t>({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<std::uint32_t>({0, 2, 1}));
|
||||
assert(greedy_algorithms::stable_matching::gale_shapley(
|
||||
secondary_preferences, primary_preferences) ==
|
||||
std::vector<std::uint32_t>({0, 2, 1}));
|
||||
|
||||
// Test Case 4
|
||||
primary_preferences = {};
|
||||
secondary_preferences = {};
|
||||
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({}));
|
||||
assert(greedy_algorithms::stable_matching::gale_shapley(
|
||||
secondary_preferences, primary_preferences) ==
|
||||
std::vector<std::uint32_t>({}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <algorithm> /// Used for std::copy
|
||||
#include <array> /// Used for std::array
|
||||
#include <cassert> /// Used for assert
|
||||
#include <cstdint>
|
||||
#include <cstring> /// Used for std::memcopy
|
||||
#include <iostream> /// Used for IO operations
|
||||
#include <string> /// Used for strings
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <algorithm> /// For std::copy
|
||||
#include <array> /// For std::array
|
||||
#include <cassert> /// For assert
|
||||
#include <cstdint>
|
||||
#include <cstring> /// For std::memcopy
|
||||
#include <iostream> /// For IO operations
|
||||
#include <string> /// For strings
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* @author [ewd00010](https://github.com/ewd00010)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for cout
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
/** Compute double factorial using iterative method
|
||||
|
||||
@@ -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 <iostream> /// for IO operations
|
||||
#include <cassert> /// for assert
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// 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;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for I/O operations
|
||||
|
||||
/**
|
||||
* @namespace
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
*
|
||||
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
|
||||
@@ -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<iostream>
|
||||
#include<vector>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* 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<uint64_t> result(2,0);
|
||||
std::vector<std::vector<uint64_t>> transition(2,std::vector<uint64_t>(2,0));
|
||||
std::vector<std::vector<uint64_t>> Identity(2,std::vector<uint64_t>(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<std::vector<uint64_t>> res(2, std::vector<uint64_t>(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<std::vector<uint64_t>> res1(2, std::vector<uint64_t>(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<uint64_t> result(2, 0);
|
||||
std::vector<std::vector<uint64_t>> transition(2,
|
||||
std::vector<uint64_t>(2, 0));
|
||||
std::vector<std::vector<uint64_t>> Identity(2, std::vector<uint64_t>(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<std::vector<uint64_t>> res(2,
|
||||
std::vector<uint64_t>(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<std::vector<uint64_t>> res1(
|
||||
2, std::vector<uint64_t>(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<<"th Fibonacci number in modulo " << mod << ": "<< fibo( n , mod) << std::endl;
|
||||
int main() {
|
||||
test();
|
||||
uint64_t mod = 1000000007;
|
||||
std::cout << "Enter the value of N: ";
|
||||
uint64_t n = 0;
|
||||
std::cin >> n;
|
||||
std::cout << n << "th Fibonacci number in modulo " << mod << ": "
|
||||
<< fibo(n, mod) << std::endl;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for log calculation
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical functions
|
||||
#include <functional> /// for passing in functions
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical functions
|
||||
#include <cstdint>
|
||||
#include <functional> /// for passing in functions
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for `std::sqrt`
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <limits> /// 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
|
||||
|
||||
@@ -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 <iostream> /// for std::cin and std::cout
|
||||
#include <cassert> /// for assert
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// 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!"<<std::endl;
|
||||
|
||||
uint16_t test_case_2 = math::largestPower(10,3);
|
||||
assert(test_case_2==4);
|
||||
std::cout<<"Test 2 Passed!"<<std::endl;
|
||||
|
||||
uint32_t test_case_3 = math::largestPower(25,5);
|
||||
assert(test_case_3==6);
|
||||
std::cout<<"Test 3 Passed!"<<std::endl;
|
||||
|
||||
uint32_t test_case_4 = math::largestPower(27,2);
|
||||
assert(test_case_4==23);
|
||||
std::cout<<"Test 4 Passed!"<<std::endl;
|
||||
|
||||
uint16_t test_case_5 = math::largestPower(7,3);
|
||||
assert(test_case_5==2);
|
||||
std::cout<<"Test 5 Passed!"<<std::endl;
|
||||
}
|
||||
*/
|
||||
static void test() {
|
||||
uint8_t test_case_1 = math::largestPower(5, 2);
|
||||
assert(test_case_1 == 3);
|
||||
std::cout << "Test 1 Passed!" << std::endl;
|
||||
|
||||
uint16_t test_case_2 = math::largestPower(10, 3);
|
||||
assert(test_case_2 == 4);
|
||||
std::cout << "Test 2 Passed!" << std::endl;
|
||||
|
||||
uint32_t test_case_3 = math::largestPower(25, 5);
|
||||
assert(test_case_3 == 6);
|
||||
std::cout << "Test 3 Passed!" << std::endl;
|
||||
|
||||
uint32_t test_case_4 = math::largestPower(27, 2);
|
||||
assert(test_case_4 == 23);
|
||||
std::cout << "Test 4 Passed!" << std::endl;
|
||||
|
||||
uint16_t test_case_5 = math::largestPower(7, 3);
|
||||
assert(test_case_5 == 2);
|
||||
std::cout << "Test 5 Passed!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
test(); // execute the tests
|
||||
int main() {
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
* @author [Ashish Daulatabad](https://github.com/AshishYUO)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector STL
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
* @author [Neha Hasija](https://github.com/neha-hasija17)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
* @author [Shri2206](https://github.com/Shri2206)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
* @see primes_up_to_billion.cpp prime_numbers.cpp
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// 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";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* @see fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // use this for MS Visual C
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* @author [muskan0719](https://github.com/muskan0719)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define _USE_MATH_DEFINES ///< required for MS Visual C++
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical-related functions
|
||||
#include <complex> /// for storing points and coefficents
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#define _USE_MATH_DEFINES //< required for MS Visual C++
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical-related functions
|
||||
#include <complex> /// for storing points and coefficents
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* \see bisection_method.cpp, false_position.cpp
|
||||
*/
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
@@ -19,9 +19,9 @@
|
||||
* There can be many such equations
|
||||
*/
|
||||
#include <cassert> /// asserting the test functions
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for using the vector container
|
||||
|
||||
/**
|
||||
* @brief The change() function is used
|
||||
* to return the updated iterative value corresponding
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/*
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
#include <array> /// for std::array
|
||||
#include <cassert> /// for assert
|
||||
#include <climits> /// for INT_MIN value
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* @brief Utility function to check the current maximum number
|
||||
* \param arr input array
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
* */
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO Operations
|
||||
#include <list> /// for std::list
|
||||
#include <unordered_map> /// for std::unordered_map
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
|
||||
#include <algorithm> /// for std::sort function
|
||||
#include <cassert> /// for std::assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
/******************************************************************************
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // use for MS Visual C++
|
||||
#else
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace search
|
||||
* @brief Search algorithms
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
#include <algorithm> /// for std::sort function
|
||||
#include <cassert> /// for std::assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -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 <cassert> /// for assert
|
||||
#include <iostream> /// for io operations, and std::pair
|
||||
#include <vector> /// for std::vector
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations, and std::pair
|
||||
#include <vector> /// 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<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matrix,
|
||||
int32_t element) {
|
||||
std::pair<uint32_t, uint32_t> saddleback(
|
||||
std::vector<std::vector<int32_t>> 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<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> 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<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matri
|
||||
*/
|
||||
static void test() {
|
||||
std::vector<std::vector<int32_t>> 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<uint32_t, uint32_t> not_found = std::make_pair(0, 0);
|
||||
std::pair<uint32_t, uint32_t> test_answer;
|
||||
// Test 1
|
||||
std::pair<uint32_t, uint32_t> answer1 = search::saddleback::saddleback(matrix, 123);
|
||||
std::pair<uint32_t, uint32_t> 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;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <algorithm> /// for std::is_sorted, std::swap
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::swap and io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <cassert> /// for std::assert
|
||||
#include <cstdint>
|
||||
#include <ctime> /// for std::time
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
@@ -23,8 +23,10 @@
|
||||
*/
|
||||
|
||||
/// header files
|
||||
|
||||
#include <algorithm> /// for collection of functions
|
||||
#include <cassert> /// for a macro called assert which can be used to verify assumptions
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -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 <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <array> /// for std::array
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <array> /// for std::array
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// 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;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
*******************************************************************************/
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <cassert> /// for std::assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -28,9 +28,10 @@
|
||||
*/
|
||||
|
||||
#include <algorithm> /// for std::is_sorted
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for std::swap and io operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for std::swap and io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector>
|
||||
@@ -50,7 +51,7 @@ namespace wiggle_sort {
|
||||
*/
|
||||
template <typename T> // this allows to have vectors of ints, double, float,
|
||||
// etc
|
||||
std::vector<T> wiggleSort(const std::vector<T> &arr) {
|
||||
std::vector<T> wiggleSort(const std::vector<T> &arr) {
|
||||
uint32_t size = arr.size();
|
||||
|
||||
std::vector<T> out(
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector STL
|
||||
#ifdef _MSC_VER
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* @author [Ritika Gupta](https://github.com/RitikaGupta8734)
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream> /// for IO operations
|
||||
#ifdef _MSC_VER
|
||||
#include <string> /// for string (use this for MS Visual C++)
|
||||
|
||||
Reference in New Issue
Block a user