mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-02 02:02:23 +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:
@@ -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