Skip to content

Commit 188a628

Browse files
Merge branch 'master' into dev/ps_ISSUE-2719
2 parents e75481a + 0ecb6bd commit 188a628

File tree

5 files changed

+254
-6
lines changed

5 files changed

+254
-6
lines changed

DIRECTORY.md

Lines changed: 3 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)

bit_manipulation/gray_code.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @brief Program to generate n-bit [Gray
3+
* code](https://en.wikipedia.org/wiki/Gray_code)
4+
*
5+
* @details
6+
* Gray code is a binary numeral system
7+
* where consecutive values differ in exactly 1 bit.
8+
* The following code offers one of many possible Gray codes
9+
* given some pre-determined number of bits.
10+
*/
11+
12+
#include <bitset> /// for gray code representation
13+
#include <cassert> /// for assert
14+
#include <iostream> /// for IO operations
15+
#include <vector> /// for vector data structure
16+
17+
/**
18+
* @namespace bit_manipulation
19+
* @brief Bit manipulation algorithms
20+
*/
21+
namespace bit_manipulation {
22+
/**
23+
* @namespace gray_code
24+
* @brief Generate n-bit Gray code
25+
*/
26+
namespace gray_code {
27+
/**
28+
* @brief The main function to generate n-bit Gray code
29+
*
30+
* @param n Number of bits
31+
* @return A vector that stores the n-bit Gray code
32+
*/
33+
std::vector<std::bitset<32>> gray_code_generation(int n) {
34+
std::vector<std::bitset<32>> gray_code = {}; // Initialise empty vector
35+
36+
// No Gray codes for non-positive values of n
37+
if (n <= 0) {
38+
return gray_code;
39+
}
40+
41+
int total_codes = 1 << n; // Number of n-bit gray codes
42+
43+
for (int i = 0; i < total_codes; i++) {
44+
int gray_num = i ^ (i >> 1); // Gray code formula
45+
gray_code.push_back(std::bitset<32>(gray_num)); // Store the value
46+
}
47+
48+
return gray_code;
49+
}
50+
} // namespace gray_code
51+
} // namespace bit_manipulation
52+
53+
/**
54+
* @brief Self-test implementation
55+
*
56+
* @returns void
57+
*/
58+
static void test() {
59+
std::vector<std::bitset<32>> gray_code_negative_1 = {};
60+
61+
std::vector<std::bitset<32>> gray_code_0 = {};
62+
63+
std::vector<std::bitset<32>> gray_code_1 = {
64+
std::bitset<32>(0), std::bitset<32>(1)
65+
};
66+
67+
std::vector<std::bitset<32>> gray_code_2 = {
68+
std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2)
69+
};
70+
71+
std::vector<std::bitset<32>> gray_code_3 = {
72+
std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
73+
std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4)
74+
};
75+
76+
std::vector<std::bitset<32>> gray_code_4 = {
77+
std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
78+
std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4),
79+
std::bitset<32>(12), std::bitset<32>(13), std::bitset<32>(15), std::bitset<32>(14),
80+
std::bitset<32>(10), std::bitset<32>(11), std::bitset<32>(9), std::bitset<32>(8)
81+
};
82+
83+
std::vector<std::bitset<32>> gray_code_5 = {
84+
std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
85+
std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4),
86+
std::bitset<32>(12), std::bitset<32>(13), std::bitset<32>(15), std::bitset<32>(14),
87+
std::bitset<32>(10), std::bitset<32>(11), std::bitset<32>(9), std::bitset<32>(8),
88+
std::bitset<32>(24), std::bitset<32>(25), std::bitset<32>(27), std::bitset<32>(26),
89+
std::bitset<32>(30), std::bitset<32>(31), std::bitset<32>(29), std::bitset<32>(28),
90+
std::bitset<32>(20), std::bitset<32>(21), std::bitset<32>(23), std::bitset<32>(22),
91+
std::bitset<32>(18), std::bitset<32>(19), std::bitset<32>(17), std::bitset<32>(16)
92+
};
93+
94+
// invalid values for n
95+
assert(bit_manipulation::gray_code::gray_code_generation(-1) == gray_code_negative_1);
96+
assert(bit_manipulation::gray_code::gray_code_generation(0) == gray_code_0);
97+
98+
// valid values for n
99+
assert(bit_manipulation::gray_code::gray_code_generation(1) == gray_code_1);
100+
assert(bit_manipulation::gray_code::gray_code_generation(2) == gray_code_2);
101+
assert(bit_manipulation::gray_code::gray_code_generation(3) == gray_code_3);
102+
assert(bit_manipulation::gray_code::gray_code_generation(4) == gray_code_4);
103+
assert(bit_manipulation::gray_code::gray_code_generation(5) == gray_code_5);
104+
}
105+
106+
/**
107+
* @brief Main function
108+
* @returns 0 on exit
109+
*/
110+
int main() {
111+
test(); //Run self-test implementation
112+
return 0;
113+
}

greedy_algorithms/gale_shapley.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* @file
3+
* @brief [Gale Shapley Algorithm](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm)
4+
* @details
5+
* This implementation utilizes the Gale-Shapley algorithm to find stable matches.
6+
*
7+
* **Gale Shapley Algorithm** aims to find a stable matching between two equally sized
8+
* sets of elements given an ordinal preference for each element. The algorithm was
9+
* introduced by David Gale and Lloyd Shapley in 1962.
10+
*
11+
* Reference:
12+
* [Wikipedia](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm)
13+
* [Wikipedia](https://en.wikipedia.org/wiki/Stable_matching_problem)
14+
*
15+
* @author [B Karthik](https://github.com/BKarthik7)
16+
*/
17+
18+
#include <iostream> /// for std::u32int_t
19+
#include <vector> /// for std::vector
20+
#include <algorithm> /// for std::find
21+
#include <cassert> /// for assert
22+
23+
/**
24+
* @namespace
25+
* @brief Greedy Algorithms
26+
*/
27+
namespace greedy_algorithms {
28+
/**
29+
* @namespace
30+
* @brief Functions for the Gale-Shapley Algorithm
31+
*/
32+
namespace stable_matching {
33+
/**
34+
* @brief The main function that finds the stable matching between two sets of elements
35+
* using the Gale-Shapley Algorithm.
36+
* @note This doesn't work on negative preferences. the preferences should be continuous integers starting from
37+
* 0 to number of preferences - 1.
38+
* @param primary_preferences the preferences of the primary set should be a 2D vector
39+
* @param secondary_preferences the preferences of the secondary set should be a 2D vector
40+
* @returns matches the stable matching between the two sets
41+
*/
42+
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) {
43+
std::uint32_t num_elements = secondary_preferences.size();
44+
std::vector<std::uint32_t> matches(num_elements, -1);
45+
std::vector<bool> is_free_primary(num_elements, true);
46+
std::vector<std::uint32_t> proposal_index(num_elements, 0); // Tracks the next secondary to propose for each primary
47+
48+
while (true) {
49+
int free_primary_index = -1;
50+
51+
// Find the next free primary
52+
for (std::uint32_t i = 0; i < num_elements; i++) {
53+
if (is_free_primary[i]) {
54+
free_primary_index = i;
55+
break;
56+
}
57+
}
58+
59+
// If no free primary is found, break the loop
60+
if (free_primary_index == -1) break;
61+
62+
// Get the next secondary to propose
63+
std::uint32_t secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
64+
proposal_index[free_primary_index]++;
65+
66+
// Get the current match of the secondary
67+
std::uint32_t current_match = matches[secondary_to_propose];
68+
69+
// If the secondary is free, match them
70+
if (current_match == -1) {
71+
matches[secondary_to_propose] = free_primary_index;
72+
is_free_primary[free_primary_index] = false;
73+
} else {
74+
// Determine if the current match should be replaced
75+
auto new_proposer_rank = std::find(secondary_preferences[secondary_to_propose].begin(),
76+
secondary_preferences[secondary_to_propose].end(),
77+
free_primary_index);
78+
auto current_match_rank = std::find(secondary_preferences[secondary_to_propose].begin(),
79+
secondary_preferences[secondary_to_propose].end(),
80+
current_match);
81+
82+
// If the new proposer is preferred over the current match
83+
if (new_proposer_rank < current_match_rank) {
84+
matches[secondary_to_propose] = free_primary_index;
85+
is_free_primary[free_primary_index] = false;
86+
is_free_primary[current_match] = true; // Current match is now free
87+
}
88+
}
89+
}
90+
91+
return matches;
92+
}
93+
} // namespace stable_matching
94+
} // namespace greedy_algorithms
95+
96+
/**
97+
* @brief Self-test implementations
98+
* @returns void
99+
*/
100+
static void tests() {
101+
// Test Case 1
102+
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}};
103+
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}};
104+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 2, 1, 3}));
105+
106+
// Test Case 2
107+
primary_preferences = {{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}};
108+
secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
109+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 3, 1, 2}));
110+
111+
// Test Case 3
112+
primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}};
113+
secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}};
114+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 2, 1}));
115+
116+
// Test Case 4
117+
primary_preferences = {};
118+
secondary_preferences = {};
119+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({}));
120+
}
121+
122+
/**
123+
* @brief Main function
124+
* @returns 0 on exit
125+
*/
126+
int main() {
127+
tests(); // Run self-test implementations
128+
return 0;
129+
}

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)

0 commit comments

Comments
 (0)