Skip to content

Commit d40099a

Browse files
Merge branch 'master' into cstdint
2 parents 030431f + f9fb58f commit d40099a

File tree

4 files changed

+131
-6
lines changed

4 files changed

+131
-6
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_set_bits.cpp)
2020
* [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)
2121
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
22+
* [Gray Code](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/gray_code.cpp)
2223
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
2324
* [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)
2425
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
@@ -161,6 +162,7 @@
161162
## Greedy Algorithms
162163
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
163164
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
165+
* [Gale Shapley](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/gale_shapley.cpp)
164166
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
165167
* [Jump Game](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/jump_game.cpp)
166168
* [Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/knapsack.cpp)
@@ -377,6 +379,7 @@
377379
* [Pigeonhole Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/pigeonhole_sort.cpp)
378380
* [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort.cpp)
379381
* [Quick Sort 3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_3.cpp)
382+
* [Quick Sort Iterative](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_iterative.cpp)
380383
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort.cpp)
381384
* [Radix Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort2.cpp)
382385
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/random_pivot_quick_sort.cpp)
@@ -396,6 +399,7 @@
396399
## Strings
397400
* [Boyer Moore](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/boyer_moore.cpp)
398401
* [Brute Force String Searching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/brute_force_string_searching.cpp)
402+
* [Duval](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/duval.cpp)
399403
* [Horspool](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/horspool.cpp)
400404
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/knuth_morris_pratt.cpp)
401405
* [Manacher Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/manacher_algorithm.cpp)

physics/ground_to_ground_projectile_motion.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include <cassert> /// for assert()
13+
#define _USE_MATH_DEFINES
1314
#include <cmath> /// for std::pow(), std::sin(), and std::cos()
1415
#include <iostream> /// for IO operations
1516

@@ -27,11 +28,12 @@ namespace ground_to_ground_projectile_motion {
2728
/**
2829
* @brief Convert radians to degrees
2930
* @param radian Angle in radians
30-
* @param PI The definition of the constant PI
3131
* @returns Angle in degrees
3232
*/
33-
double degrees_to_radians(double radian, double PI = 3.14) {
34-
return (radian * (PI / 180));
33+
34+
double degrees_to_radians(double degrees){
35+
double radians = degrees * (M_PI / 180);
36+
return radians;
3537
}
3638

3739
/**

search/binary_search.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
* algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm)
55
* @details
66
* Binary search is a search algorithm that finds the position of a target value
7-
* within a sorted array. Binary search compares the target value to the middle
8-
* element of the array. If they are not equal, the half in which the target
7+
* within a sorted array.Just like looking for a word in a dictionary, in binary search we compare the target value to the middle
8+
* element of the array. If they are not equal, then the half in which the target
99
* cannot lie is eliminated and the search continues on the remaining half,
1010
* again taking the middle element to compare to the target value, and repeating
1111
* this until the target value is found. If the search ends with the remaining
1212
* half being empty, the target is not in the array.
1313
*
1414
* ### Implementation
1515
*
16-
* Binary search works on sorted arrays. Binary search begins by comparing an
16+
* Binary search works on sorted arrays. It begins by comparing an
1717
* element in the middle of the array with the target value. If the target value
1818
* matches the element, its position in the array is returned. If the target
1919
* value is less than the element, the search continues in the lower half of
@@ -28,6 +28,7 @@
2828
* Worst-case time complexity O(log n)
2929
* Best-case time complexity O(1)
3030
* Average time complexity O(log n)
31+
* space complexity 0(1)
3132
* Worst-case space complexity 0(1)
3233
*
3334
* @author [Lajat Manekar](https://github.com/Lazeeez)

strings/duval.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @file duval.cpp
3+
* @brief Implementation of [Duval's algorithm](https://en.wikipedia.org/wiki/Lyndon_word).
4+
*
5+
* @details
6+
* Duval's algorithm is an algorithm to find the lexicographically smallest
7+
* rotation of a string. It is based on the concept of Lyndon words.
8+
* Lyndon words are defined as the lexicographically smallest string in a
9+
* rotation equivalence class. A rotation equivalence class is a set of strings
10+
* that can be obtained by rotating a string. For example, the rotation
11+
* equivalence class of "abc" is {"abc", "bca", "cab"}. The lexicographically
12+
* smallest string in this class is "abc".
13+
*
14+
* Duval's algorithm works by iterating over the string and finding the
15+
* smallest rotation of the string that is a Lyndon word. This is done by
16+
* comparing the string with its suffixes and finding the smallest suffix that
17+
* is lexicographically smaller than the string. This suffix is then added to
18+
* the result and the process is repeated with the remaining string.
19+
* The algorithm has a time complexity of O(n) where n is the length of the
20+
* string.
21+
*
22+
* @note While Lyndon words are described in the context of strings,
23+
* Duval's algorithm can be used to find the lexicographically smallest cyclic
24+
* shift of any sequence of comparable elements.
25+
*
26+
* @author [Amine Ghoussaini](https://github.com/aminegh20)
27+
*/
28+
29+
#include <array> /// for std::array
30+
#include <cassert> /// for assert
31+
#include <cstddef> /// for std::size_t
32+
#include <deque> /// for std::deque
33+
#include <iostream> /// for std::cout and std::endl
34+
#include <string> /// for std::string
35+
#include <vector> /// for std::vector
36+
37+
/**
38+
* @brief string manipulation algorithms
39+
* @namespace
40+
*/
41+
namespace string {
42+
/**
43+
* @brief Find the lexicographically smallest cyclic shift of a sequence.
44+
* @tparam T type of the sequence
45+
* @param s the sequence
46+
* @returns the 0-indexed position of the least cyclic shift of the sequence
47+
*/
48+
template <typename T>
49+
size_t duval(const T& s) {
50+
size_t n = s.size();
51+
size_t i = 0, ans = 0;
52+
while (i < n) {
53+
ans = i;
54+
size_t j = i + 1, k = i;
55+
while (j < (n + n) && s[j % n] >= s[k % n]) {
56+
if (s[k % n] < s[j % n]) {
57+
k = i;
58+
} else {
59+
k++;
60+
}
61+
j++;
62+
}
63+
while (i <= k) {
64+
i += j - k;
65+
}
66+
}
67+
return ans;
68+
// returns 0-indexed position of the least cyclic shift
69+
}
70+
71+
} // namespace string
72+
73+
/**
74+
* @brief self test implementation
75+
* returns void
76+
*/
77+
static void test() {
78+
using namespace string;
79+
80+
// Test 1
81+
std::string s1 = "abcab";
82+
assert(duval(s1) == 3);
83+
84+
// Test 2
85+
std::string s2 = "011100";
86+
assert(duval(s2) == 4);
87+
88+
// Test 3
89+
std::vector<int> v = {5, 2, 1, 3, 4};
90+
assert(duval(v) == 2);
91+
92+
// Test 4
93+
std::array<int, 5> a = {1, 2, 3, 4, 5};
94+
assert(duval(a) == 0);
95+
96+
// Test 5
97+
std::deque<char> d = {'a', 'z', 'c', 'a', 'b'};
98+
assert(duval(d) == 3);
99+
100+
// Test 6
101+
std::string s3;
102+
assert(duval(s3) == 0);
103+
104+
// Test 7
105+
std::vector<int> v2 = {5, 2, 1, 3, -4};
106+
assert(duval(v2) == 4);
107+
108+
std::cout << "All tests passed!" << std::endl;
109+
}
110+
111+
/**
112+
* @brief main function
113+
* @returns 0 on exit
114+
*/
115+
int main() {
116+
test(); // run self test implementations
117+
return 0;
118+
}

0 commit comments

Comments
 (0)