Skip to content

Commit

Permalink
Merge branch 'master' into cstdint
Browse files Browse the repository at this point in the history
  • Loading branch information
realstealthninja authored Oct 6, 2024
2 parents 030431f + f9fb58f commit d40099a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 6 deletions.
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_set_bits.cpp)
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
* [Gray Code](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/gray_code.cpp)
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
* [Next Higher Number With Same Number Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp)
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
Expand Down Expand Up @@ -161,6 +162,7 @@
## Greedy Algorithms
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
* [Gale Shapley](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/gale_shapley.cpp)
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
* [Jump Game](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/jump_game.cpp)
* [Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/knapsack.cpp)
Expand Down Expand Up @@ -377,6 +379,7 @@
* [Pigeonhole Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/pigeonhole_sort.cpp)
* [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort.cpp)
* [Quick Sort 3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_3.cpp)
* [Quick Sort Iterative](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_iterative.cpp)
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort.cpp)
* [Radix Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort2.cpp)
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/random_pivot_quick_sort.cpp)
Expand All @@ -396,6 +399,7 @@
## Strings
* [Boyer Moore](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/boyer_moore.cpp)
* [Brute Force String Searching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/brute_force_string_searching.cpp)
* [Duval](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/duval.cpp)
* [Horspool](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/horspool.cpp)
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/knuth_morris_pratt.cpp)
* [Manacher Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/manacher_algorithm.cpp)
Expand Down
8 changes: 5 additions & 3 deletions physics/ground_to_ground_projectile_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <cassert> /// for assert()
#define _USE_MATH_DEFINES
#include <cmath> /// for std::pow(), std::sin(), and std::cos()
#include <iostream> /// for IO operations

Expand All @@ -27,11 +28,12 @@ namespace ground_to_ground_projectile_motion {
/**
* @brief Convert radians to degrees
* @param radian Angle in radians
* @param PI The definition of the constant PI
* @returns Angle in degrees
*/
double degrees_to_radians(double radian, double PI = 3.14) {
return (radian * (PI / 180));

double degrees_to_radians(double degrees){
double radians = degrees * (M_PI / 180);
return radians;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions search/binary_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
* algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm)
* @details
* Binary search is a search algorithm that finds the position of a target value
* within a sorted array. Binary search compares the target value to the middle
* element of the array. If they are not equal, the half in which the target
* within a sorted array.Just like looking for a word in a dictionary, in binary search we compare the target value to the middle
* element of the array. If they are not equal, then the half in which the target
* cannot lie is eliminated and the search continues on the remaining half,
* again taking the middle element to compare to the target value, and repeating
* this until the target value is found. If the search ends with the remaining
* half being empty, the target is not in the array.
*
* ### Implementation
*
* Binary search works on sorted arrays. Binary search begins by comparing an
* Binary search works on sorted arrays. It begins by comparing an
* element in the middle of the array with the target value. If the target value
* matches the element, its position in the array is returned. If the target
* value is less than the element, the search continues in the lower half of
Expand All @@ -28,6 +28,7 @@
* Worst-case time complexity O(log n)
* Best-case time complexity O(1)
* Average time complexity O(log n)
* space complexity 0(1)
* Worst-case space complexity 0(1)
*
* @author [Lajat Manekar](https://github.com/Lazeeez)
Expand Down
118 changes: 118 additions & 0 deletions strings/duval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* @file duval.cpp
* @brief Implementation of [Duval's algorithm](https://en.wikipedia.org/wiki/Lyndon_word).
*
* @details
* Duval's algorithm is an algorithm to find the lexicographically smallest
* rotation of a string. It is based on the concept of Lyndon words.
* Lyndon words are defined as the lexicographically smallest string in a
* rotation equivalence class. A rotation equivalence class is a set of strings
* that can be obtained by rotating a string. For example, the rotation
* equivalence class of "abc" is {"abc", "bca", "cab"}. The lexicographically
* smallest string in this class is "abc".
*
* Duval's algorithm works by iterating over the string and finding the
* smallest rotation of the string that is a Lyndon word. This is done by
* comparing the string with its suffixes and finding the smallest suffix that
* is lexicographically smaller than the string. This suffix is then added to
* the result and the process is repeated with the remaining string.
* The algorithm has a time complexity of O(n) where n is the length of the
* string.
*
* @note While Lyndon words are described in the context of strings,
* Duval's algorithm can be used to find the lexicographically smallest cyclic
* shift of any sequence of comparable elements.
*
* @author [Amine Ghoussaini](https://github.com/aminegh20)
*/

#include <array> /// for std::array
#include <cassert> /// for assert
#include <cstddef> /// for std::size_t
#include <deque> /// for std::deque
#include <iostream> /// for std::cout and std::endl
#include <string> /// for std::string
#include <vector> /// for std::vector

/**
* @brief string manipulation algorithms
* @namespace
*/
namespace string {
/**
* @brief Find the lexicographically smallest cyclic shift of a sequence.
* @tparam T type of the sequence
* @param s the sequence
* @returns the 0-indexed position of the least cyclic shift of the sequence
*/
template <typename T>
size_t duval(const T& s) {
size_t n = s.size();
size_t i = 0, ans = 0;
while (i < n) {
ans = i;
size_t j = i + 1, k = i;
while (j < (n + n) && s[j % n] >= s[k % n]) {
if (s[k % n] < s[j % n]) {
k = i;
} else {
k++;
}
j++;
}
while (i <= k) {
i += j - k;
}
}
return ans;
// returns 0-indexed position of the least cyclic shift
}

} // namespace string

/**
* @brief self test implementation
* returns void
*/
static void test() {
using namespace string;

// Test 1
std::string s1 = "abcab";
assert(duval(s1) == 3);

// Test 2
std::string s2 = "011100";
assert(duval(s2) == 4);

// Test 3
std::vector<int> v = {5, 2, 1, 3, 4};
assert(duval(v) == 2);

// Test 4
std::array<int, 5> a = {1, 2, 3, 4, 5};
assert(duval(a) == 0);

// Test 5
std::deque<char> d = {'a', 'z', 'c', 'a', 'b'};
assert(duval(d) == 3);

// Test 6
std::string s3;
assert(duval(s3) == 0);

// Test 7
std::vector<int> v2 = {5, 2, 1, 3, -4};
assert(duval(v2) == 4);

std::cout << "All tests passed!" << std::endl;
}

/**
* @brief main function
* @returns 0 on exit
*/
int main() {
test(); // run self test implementations
return 0;
}

0 comments on commit d40099a

Please sign in to comment.