mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-13 16:29:48 +08:00
feat: Add ncr mod p code (#1325)
* feat: Add ncr mod p code (#1323) * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added all functions inside a class + added more asserts * updating DIRECTORY.md * clang-format and clang-tidy fixes forf6df24a5* Replace int64_t to uint64_t + add namespace + detailed documentation * clang-format and clang-tidy fixes fore09a0579* Add extra namespace + add const& in function arguments * clang-format and clang-tidy fixes for8111f881* Update ncr_modulo_p.cpp * clang-format and clang-tidy fixes for2ad2f721* Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for5b69ba5c* updating DIRECTORY.md * clang-format and clang-tidy fixes fora8401d4bCo-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
|
||||
* @brief Implementation of [Cycle
|
||||
* sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
|
||||
*
|
||||
* @details
|
||||
* Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in best cas and works in \f$O(n^2)\f$ in worst case.
|
||||
* If a element is already at its correct position, do nothing.
|
||||
* If a element is not at its correct position, we then need to move it to its correct position by computing the correct positions.Therefore, we should make sure the duplicate elements.
|
||||
* Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in best cas
|
||||
* and works in \f$O(n^2)\f$ in worst case. If a element is already at its
|
||||
* correct position, do nothing. If a element is not at its correct position,
|
||||
* we then need to move it to its correct position by computing the correct
|
||||
* positions.Therefore, we should make sure the duplicate elements.
|
||||
*
|
||||
* @author [TsungHan Ho](https://github.com/dalaoqi)
|
||||
*/
|
||||
|
||||
#include <algorithm> /// for std::is_sorted, std::swap
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <algorithm> /// for std::is_sorted, std::swap
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
@@ -22,9 +25,10 @@
|
||||
namespace sorting {
|
||||
/**
|
||||
* @namespace cycle_sort
|
||||
* @brief Functions for [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm
|
||||
* @brief Functions for [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort)
|
||||
* algorithm
|
||||
*/
|
||||
namespace cycle_sort {
|
||||
namespace cycle_sort {
|
||||
/**
|
||||
* @brief The main function implements cycleSort
|
||||
* @tparam T type of array
|
||||
@@ -38,7 +42,8 @@ std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
||||
// initialize item
|
||||
T item = arr[cycle_start];
|
||||
|
||||
// Count the number of elements smaller than item, this number is the correct index of item.
|
||||
// Count the number of elements smaller than item, this number is the
|
||||
// correct index of item.
|
||||
int pos = cycle_start;
|
||||
for (size_t i = cycle_start + 1; i < arr.size(); i++) {
|
||||
if (arr[i] < item) {
|
||||
@@ -71,18 +76,19 @@ std::vector<T> cycleSort(const std::vector<T> &in_arr) {
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
} // namespace cycle_sort
|
||||
} // namespace sorting
|
||||
} // namespace cycle_sort
|
||||
} // namespace sorting
|
||||
|
||||
/**
|
||||
* @brief Test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
// [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362, 500, 506, 951]
|
||||
// [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362,
|
||||
// 500, 506, 951]
|
||||
std::vector<int> array1 = {506, 48, 123, 79, 0, 362, 951, 500, 0};
|
||||
std::cout << "Test 1... ";
|
||||
std::vector<int> arr1 = sorting::cycle_sort::cycleSort(array1);
|
||||
std::vector<int> arr1 = sorting::cycle_sort::cycleSort(array1);
|
||||
assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
|
||||
std::cout << "passed" << std::endl;
|
||||
|
||||
|
||||
@@ -3,14 +3,13 @@
|
||||
* @brief Algorithm of [Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
|
||||
* @author [Suyash Jaiswal](https://github.com/Suyashjaiswal)
|
||||
* @details
|
||||
* Sort the vector of unsigned integers using radix sort i.e. sorting digit by digit
|
||||
* using [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) as subroutine.
|
||||
* Running time of radix sort is O(d*(n+b)) where b is the base for representing
|
||||
* numbers and d in the max digits in input integers and n is number of unsigned integers.
|
||||
* consider example for n = 5, aray elements = 432,234,143,332,123
|
||||
* sorting digit by digit
|
||||
* sorting according to
|
||||
* 1) 1st digit place
|
||||
* Sort the vector of unsigned integers using radix sort i.e. sorting digit by
|
||||
* digit using [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) as
|
||||
* subroutine. Running time of radix sort is O(d*(n+b)) where b is the base for
|
||||
* representing numbers and d in the max digits in input integers and n is
|
||||
* number of unsigned integers. consider example for n = 5, aray elements =
|
||||
* 432,234,143,332,123 sorting digit by digit sorting according to 1) 1st digit
|
||||
* place
|
||||
* => 432, 332, 143, 123, 234
|
||||
*
|
||||
* 2) 2nd digit place
|
||||
@@ -21,76 +20,79 @@
|
||||
*
|
||||
* using count sort at each step, which is stable.
|
||||
* stable => already sorted according to previous digits.
|
||||
*/
|
||||
*/
|
||||
|
||||
/// header files
|
||||
#include <algorithm> /// for collection of functions
|
||||
#include <cassert> /// for a macro called assert which can be used to verify assumptions
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <algorithm> /// for collection of functions
|
||||
#include <cassert> /// for a macro called assert which can be used to verify assumptions
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
* @brief Sorting algorithms
|
||||
*/
|
||||
namespace sorting {
|
||||
/**
|
||||
* @namespace radix_sort
|
||||
* @brief Functions for [Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
|
||||
* algorithm
|
||||
*/
|
||||
namespace radix_sort {
|
||||
/**
|
||||
* @brief Function to sort vector according to current digit using stable
|
||||
* sorting.
|
||||
* @param cur_digit - sort according to the cur_digit
|
||||
* @param ar - vector to be sorted
|
||||
* @returns std::vector sorted till ith digit
|
||||
*/
|
||||
std::vector<uint64_t> step_ith(uint16_t cur_digit, const std::vector<uint64_t>& ar) { // sorting according to current digit.
|
||||
int n = ar.size();
|
||||
std::vector<uint32_t> position(10, 0);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
position[(ar[i] / cur_digit) %
|
||||
10]++; // counting frequency of 0-9 at cur_digit.
|
||||
}
|
||||
int cur = 0;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
int a = position[i];
|
||||
position[i] = cur; // assingning starting position of 0-9.
|
||||
cur += a;
|
||||
}
|
||||
std::vector<uint64_t> temp(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
temp[position[(ar[i] / cur_digit) % 10]] =
|
||||
ar[i]; // storing ar[i] in ar[i]'s cur_digit expected position of
|
||||
// this step.
|
||||
position[(ar[i] / cur_digit) %
|
||||
10]++; // incrementing ar[i]'s cur_digit position by 1, as
|
||||
// current place used by ar[i].
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
/**
|
||||
* @brief Function to sort vector digit by digit.
|
||||
* @param ar - vector to be sorted
|
||||
* @returns sorted vector
|
||||
*/
|
||||
std::vector<uint64_t> radix(const std::vector<uint64_t>& ar) {
|
||||
uint64_t max_ele = *max_element(ar.begin(), ar.end()); // returns the max element.
|
||||
std::vector<uint64_t> temp = ar;
|
||||
for (int i = 1; max_ele / i > 0;
|
||||
i *= 10) { // loop breaks when i > max_ele because no further digits
|
||||
// left to makes changes in aray.
|
||||
temp = step_ith(i,temp);
|
||||
}
|
||||
for (uint64_t i : temp) {
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return temp;
|
||||
}
|
||||
} // namespace radix_sort
|
||||
/**
|
||||
* @namespace radix_sort
|
||||
* @brief Functions for [Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
|
||||
* algorithm
|
||||
*/
|
||||
namespace radix_sort {
|
||||
/**
|
||||
* @brief Function to sort vector according to current digit using stable
|
||||
* sorting.
|
||||
* @param cur_digit - sort according to the cur_digit
|
||||
* @param ar - vector to be sorted
|
||||
* @returns std::vector sorted till ith digit
|
||||
*/
|
||||
std::vector<uint64_t> step_ith(
|
||||
uint16_t cur_digit,
|
||||
const std::vector<uint64_t>& ar) { // sorting according to current digit.
|
||||
int n = ar.size();
|
||||
std::vector<uint32_t> position(10, 0);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
position[(ar[i] / cur_digit) %
|
||||
10]++; // counting frequency of 0-9 at cur_digit.
|
||||
}
|
||||
int cur = 0;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
int a = position[i];
|
||||
position[i] = cur; // assingning starting position of 0-9.
|
||||
cur += a;
|
||||
}
|
||||
std::vector<uint64_t> temp(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
temp[position[(ar[i] / cur_digit) % 10]] =
|
||||
ar[i]; // storing ar[i] in ar[i]'s cur_digit expected position of
|
||||
// this step.
|
||||
position[(ar[i] / cur_digit) %
|
||||
10]++; // incrementing ar[i]'s cur_digit position by 1, as
|
||||
// current place used by ar[i].
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
/**
|
||||
* @brief Function to sort vector digit by digit.
|
||||
* @param ar - vector to be sorted
|
||||
* @returns sorted vector
|
||||
*/
|
||||
std::vector<uint64_t> radix(const std::vector<uint64_t>& ar) {
|
||||
uint64_t max_ele =
|
||||
*max_element(ar.begin(), ar.end()); // returns the max element.
|
||||
std::vector<uint64_t> temp = ar;
|
||||
for (int i = 1; max_ele / i > 0;
|
||||
i *= 10) { // loop breaks when i > max_ele because no further digits
|
||||
// left to makes changes in aray.
|
||||
temp = step_ith(i, temp);
|
||||
}
|
||||
for (uint64_t i : temp) {
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return temp;
|
||||
}
|
||||
} // namespace radix_sort
|
||||
} // namespace sorting
|
||||
|
||||
/**
|
||||
@@ -104,7 +106,7 @@ static void tests() {
|
||||
assert(std::is_sorted(ar1.begin(), ar1.end()));
|
||||
/// Test 2
|
||||
std::vector<uint64_t> ar2 = {213, 3214, 123, 111, 112, 142,
|
||||
133, 132, 32, 12, 113};
|
||||
133, 132, 32, 12, 113};
|
||||
ar2 = sorting::radix_sort::radix(ar2);
|
||||
assert(std::is_sorted(ar2.begin(), ar2.end()));
|
||||
}
|
||||
|
||||
@@ -17,89 +17,86 @@
|
||||
* arr = [2,8,9,1,7], after wiggle sort arr will become equal to [8,2,9,1,7]
|
||||
*/
|
||||
|
||||
#include <iostream> /// for io operations
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <ctime>
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
* @brief Sorting algorithms
|
||||
*/
|
||||
namespace sorting {
|
||||
/**
|
||||
* @namespace wiggle_sort
|
||||
* @brief Functions for [Wiggle Sort](https://leetcode.com/problems/wiggle-sort-ii/) algorithm
|
||||
*/
|
||||
namespace wiggle_sort {
|
||||
/**
|
||||
* @namespace wiggle_sort
|
||||
* @brief Functions for [Wiggle
|
||||
* Sort](https://leetcode.com/problems/wiggle-sort-ii/) algorithm
|
||||
*/
|
||||
namespace wiggle_sort {
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Function used for sorting the elements in wave form.
|
||||
* @details
|
||||
* Checking whether the even indexed elements are greater than
|
||||
* their adjacent odd elements.
|
||||
* Traversing all even indexed elements of the input arr.
|
||||
* If current element is smaller than the previous odd element, swap them.
|
||||
* If current element is smaller than the next odd element, swap them.
|
||||
*
|
||||
* @param arr input array (unsorted elements)
|
||||
*
|
||||
*/
|
||||
template<typename T> // this allows to have vectors of ints, double, float, etc
|
||||
std::vector<T> wiggleSort(const std::vector<T>& arr) {
|
||||
/**
|
||||
*
|
||||
* @brief Function used for sorting the elements in wave form.
|
||||
* @details
|
||||
* Checking whether the even indexed elements are greater than
|
||||
* their adjacent odd elements.
|
||||
* Traversing all even indexed elements of the input arr.
|
||||
* If current element is smaller than the previous odd element, swap them.
|
||||
* If current element is smaller than the next odd element, swap them.
|
||||
*
|
||||
* @param arr input array (unsorted elements)
|
||||
*
|
||||
*/
|
||||
template <typename T> // this allows to have vectors of ints, double, float,
|
||||
// etc
|
||||
std::vector<T> wiggleSort(const std::vector<T> &arr) {
|
||||
uint32_t size = arr.size();
|
||||
|
||||
uint32_t size = arr.size();
|
||||
|
||||
std::vector<T> out(arr); // create a copy of input vector. this way, the original input vector does not get modified. a sorted array is is returned.
|
||||
|
||||
for(int i = 0; i < size ; i +=2) {
|
||||
|
||||
if(i > 0 && out[i-1] > out[i]) {
|
||||
std::swap(out[i],out[i-1]); //swapping the two values
|
||||
}
|
||||
|
||||
if(i < size - 1 && out[i] < out[i+1]) {
|
||||
std::swap(out[i],out[i+1]); //swapping the two values
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return out; //returns the sorted vector
|
||||
std::vector<T> out(
|
||||
arr); // create a copy of input vector. this way, the original input
|
||||
// vector does not get modified. a sorted array is is returned.
|
||||
|
||||
for (int i = 0; i < size; i += 2) {
|
||||
if (i > 0 && out[i - 1] > out[i]) {
|
||||
std::swap(out[i], out[i - 1]); // swapping the two values
|
||||
}
|
||||
} // namespace wiggle_sort
|
||||
} // namespace sorting
|
||||
|
||||
if (i < size - 1 && out[i] < out[i + 1]) {
|
||||
std::swap(out[i], out[i + 1]); // swapping the two values
|
||||
}
|
||||
}
|
||||
|
||||
return out; // returns the sorted vector
|
||||
}
|
||||
} // namespace wiggle_sort
|
||||
} // namespace sorting
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Utility function used for printing the elements.
|
||||
* Prints elements of the array after they're sorted using wiggle sort algorithm.
|
||||
* Prints elements of the array after they're sorted using wiggle sort
|
||||
* algorithm.
|
||||
*
|
||||
* @param arr array containing the sorted elements
|
||||
*
|
||||
*/
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
static void displayElements(const std::vector<T> &arr) {
|
||||
|
||||
uint32_t size = arr.size();
|
||||
|
||||
std::cout << "Sorted elements are as follows: ";
|
||||
|
||||
std::cout << "[";
|
||||
|
||||
for(int i = 0 ; i < size ; i++ ) {
|
||||
|
||||
std::cout << arr[i] ;
|
||||
if(i != size - 1) {
|
||||
std::cout << ", " ;
|
||||
for (int i = 0; i < size; i++) {
|
||||
std::cout << arr[i];
|
||||
if (i != size - 1) {
|
||||
std::cout << ", ";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::cout << "]"<<std::endl;
|
||||
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,11 +104,10 @@ static void displayElements(const std::vector<T> &arr) {
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
|
||||
std::srand(std::time(nullptr)); // initialize random number generator
|
||||
std::srand(std::time(nullptr)); // initialize random number generator
|
||||
|
||||
std::vector<float> data1(100);
|
||||
for (auto &d: data1) { // generate random numbers between -5.0 and 4.99
|
||||
for (auto &d : data1) { // generate random numbers between -5.0 and 4.99
|
||||
d = float(std::rand() % 1000 - 500) / 100.f;
|
||||
}
|
||||
|
||||
@@ -119,12 +115,12 @@ static void test() {
|
||||
|
||||
displayElements(sorted);
|
||||
|
||||
for(uint32_t j = 0; j < data1.size(); j+=2) {
|
||||
assert(data1[j] <= data1[j+1] && data1[j+1] >= data1[j+2]); // check the validation condition
|
||||
for (uint32_t j = 0; j < data1.size(); j += 2) {
|
||||
assert(data1[j] <= data1[j + 1] &&
|
||||
data1[j + 1] >= data1[j + 2]); // check the validation condition
|
||||
}
|
||||
|
||||
std::cout << "Test 1 passed\n";
|
||||
|
||||
}
|
||||
|
||||
/** Driver Code */
|
||||
|
||||
Reference in New Issue
Block a user