Skip to content

Commit b3a0070

Browse files
LazeeezPanquesito7github-actionsmishraabhinn
authored
feat: Reworked/updated sorting/selection_sort.cpp. (#1613)
* Reworked selection_sort.cpp with fixes. * Added Recursive implementation for tree traversing * Fix #2 * Delete recursive_tree_traversals.cpp * Update selection_sort.cpp * Changes done in selection_sort_iterative.cpp * updating DIRECTORY.md * clang-format and clang-tidy fixes for 4681e4f * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <[email protected]> * Update selection_sort_iterative.cpp * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/selection_sort_iterative.cpp Co-authored-by: David Leal <[email protected]> * clang-format and clang-tidy fixes for ca2a7c6 * Finished changes requested by ayaankhan98. * Reworked on changes. * clang-format and clang-tidy fixes for f79b79b * Corrected errors. * Fix #2 * Fix #3 * Major Fix #3 * clang-format and clang-tidy fixes for 79341db * clang-format and clang-tidy fixes for 9bdf2ce * Update selection_sort_iterative.cpp * clang-format and clang-tidy fixes for 9833d7a * clang-format and clang-tidy fixes for b772646 Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Abhinn Mishra <[email protected]>
1 parent 4d884b0 commit b3a0070

10 files changed

+324
-210
lines changed

DIRECTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
* [Radix Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/radix_sort2.cpp)
339339
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/random_pivot_quick_sort.cpp)
340340
* [Recursive Bubble Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/recursive_bubble_sort.cpp)
341-
* [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/selection_sort.cpp)
341+
* [Selection Sort Iterative](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/selection_sort_iterative.cpp)
342342
* [Selection Sort Recursive](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/selection_sort_recursive.cpp)
343343
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort.cpp)
344344
* [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp)

bit_manipulation/count_of_set_bits.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* integer.
66
*
77
* @details
8-
* We are given an integer number. We need to calculate the number of set bits in it.
8+
* We are given an integer number. We need to calculate the number of set bits
9+
* in it.
910
*
1011
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
1112
* set bit in computer terms.
@@ -15,7 +16,7 @@
1516
* @author [Prashant Thakur](https://github.com/prashant-th18)
1617
*/
1718
#include <cassert> /// for assert
18-
#include <iostream> /// for IO operations
19+
#include <iostream> /// for IO operations
1920
/**
2021
* @namespace bit_manipulation
2122
* @brief Bit manipulation algorithms
@@ -33,21 +34,21 @@ namespace count_of_set_bits {
3334
* @param n is the number whose set bit will be counted
3435
* @returns total number of set-bits in the binary representation of number `n`
3536
*/
36-
std::uint64_t countSetBits(std :: int64_t n) { // int64_t is preferred over int so that
37-
// no Overflow can be there.
37+
std::uint64_t countSetBits(
38+
std ::int64_t n) { // int64_t is preferred over int so that
39+
// no Overflow can be there.
3840

39-
int count = 0; // "count" variable is used to count number of set-bits('1') in
40-
// binary representation of number 'n'
41-
while (n != 0)
42-
{
41+
int count = 0; // "count" variable is used to count number of set-bits('1')
42+
// in binary representation of number 'n'
43+
while (n != 0) {
4344
++count;
4445
n = (n & (n - 1));
4546
}
4647
return count;
4748
// Why this algorithm is better than the standard one?
4849
// Because this algorithm runs the same number of times as the number of
49-
// set-bits in it. Means if my number is having "3" set bits, then this while loop
50-
// will run only "3" times!!
50+
// set-bits in it. Means if my number is having "3" set bits, then this
51+
// while loop will run only "3" times!!
5152
}
5253
} // namespace count_of_set_bits
5354
} // namespace bit_manipulation

ciphers/atbash_cipher.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*/
2323
namespace ciphers {
2424
/** \namespace atbash
25-
* \brief Functions for the [Atbash Cipher](https://en.wikipedia.org/wiki/Atbash) implementation
25+
* \brief Functions for the [Atbash
26+
* Cipher](https://en.wikipedia.org/wiki/Atbash) implementation
2627
*/
2728
namespace atbash {
2829
std::map<char, char> atbash_cipher_map = {
@@ -43,7 +44,7 @@ std::map<char, char> atbash_cipher_map = {
4344
* @param text Plaintext to be encrypted
4445
* @returns encoded or decoded string
4546
*/
46-
std::string atbash_cipher(std::string text) {
47+
std::string atbash_cipher(const std::string& text) {
4748
std::string result;
4849
for (char letter : text) {
4950
result += atbash_cipher_map[letter];

data_structures/dsu_path_compression.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void test1() {
184184
* @returns void
185185
*/
186186
static void test2() {
187-
// the minimum, maximum, and size of the set
187+
// the minimum, maximum, and size of the set
188188
uint64_t n = 10; ///< number of items
189189
dsu d(n + 1); ///< object of class disjoint sets
190190
// set 1

data_structures/stack_using_queue.cpp

+73-81
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* @details
44
* Using 2 Queues inside the Stack class, we can easily implement Stack
55
* data structure with heavy computation in push function.
6-
*
7-
* References used: [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue)
6+
*
7+
* References used:
8+
* [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue)
89
* @author [tushar2407](https://github.com/tushar2407)
910
*/
10-
#include <iostream> /// for IO operations
11-
#include <queue> /// for queue data structure
12-
#include <cassert> /// for assert
11+
#include <cassert> /// for assert
12+
#include <iostream> /// for IO operations
13+
#include <queue> /// for queue data structure
1314

1415
/**
1516
* @namespace data_strcutres
@@ -18,97 +19,89 @@
1819
namespace data_structures {
1920
/**
2021
* @namespace stack_using_queue
21-
* @brief Functions for the [Stack Using Queue](https://www.studytonight.com/data-structures/stack-using-queue) implementation
22+
* @brief Functions for the [Stack Using
23+
* Queue](https://www.studytonight.com/data-structures/stack-using-queue)
24+
* implementation
2225
*/
2326
namespace stack_using_queue {
27+
/**
28+
* @brief Stack Class implementation for basic methods of Stack Data Structure.
29+
*/
30+
struct Stack {
31+
std::queue<int64_t> main_q; ///< stores the current state of the stack
32+
std::queue<int64_t> auxiliary_q; ///< used to carry out intermediate
33+
///< operations to implement stack
34+
uint32_t current_size = 0; ///< stores the current size of the stack
35+
2436
/**
25-
* @brief Stack Class implementation for basic methods of Stack Data Structure.
37+
* Returns the top most element of the stack
38+
* @returns top element of the queue
2639
*/
27-
struct Stack
28-
{
29-
std::queue<int64_t> main_q; ///< stores the current state of the stack
30-
std::queue<int64_t> auxiliary_q; ///< used to carry out intermediate operations to implement stack
31-
uint32_t current_size = 0; ///< stores the current size of the stack
32-
33-
/**
34-
* Returns the top most element of the stack
35-
* @returns top element of the queue
36-
*/
37-
int top()
38-
{
39-
return main_q.front();
40-
}
40+
int top() { return main_q.front(); }
4141

42-
/**
43-
* @brief Inserts an element to the top of the stack.
44-
* @param val the element that will be inserted into the stack
45-
* @returns void
46-
*/
47-
void push(int val)
48-
{
49-
auxiliary_q.push(val);
50-
while(!main_q.empty())
51-
{
52-
auxiliary_q.push(main_q.front());
53-
main_q.pop();
54-
}
55-
swap(main_q, auxiliary_q);
56-
current_size++;
57-
}
58-
59-
/**
60-
* @brief Removes the topmost element from the stack
61-
* @returns void
62-
*/
63-
void pop()
64-
{
65-
if(main_q.empty()) {
66-
return;
67-
}
42+
/**
43+
* @brief Inserts an element to the top of the stack.
44+
* @param val the element that will be inserted into the stack
45+
* @returns void
46+
*/
47+
void push(int val) {
48+
auxiliary_q.push(val);
49+
while (!main_q.empty()) {
50+
auxiliary_q.push(main_q.front());
6851
main_q.pop();
69-
current_size--;
7052
}
53+
swap(main_q, auxiliary_q);
54+
current_size++;
55+
}
7156

72-
/**
73-
* @brief Utility function to return the current size of the stack
74-
* @returns current size of stack
75-
*/
76-
int size()
77-
{
78-
return current_size;
57+
/**
58+
* @brief Removes the topmost element from the stack
59+
* @returns void
60+
*/
61+
void pop() {
62+
if (main_q.empty()) {
63+
return;
7964
}
80-
};
65+
main_q.pop();
66+
current_size--;
67+
}
68+
69+
/**
70+
* @brief Utility function to return the current size of the stack
71+
* @returns current size of stack
72+
*/
73+
int size() { return current_size; }
74+
};
8175
} // namespace stack_using_queue
8276
} // namespace data_structures
8377

8478
/**
8579
* @brief Self-test implementations
8680
* @returns void
8781
*/
88-
static void test()
89-
{
82+
static void test() {
9083
data_structures::stack_using_queue::Stack s;
91-
s.push(1); /// insert an element into the stack
92-
s.push(2); /// insert an element into the stack
93-
s.push(3); /// insert an element into the stack
94-
95-
assert(s.size()==3); /// size should be 3
96-
97-
assert(s.top()==3); /// topmost element in the stack should be 3
98-
99-
s.pop(); /// remove the topmost element from the stack
100-
assert(s.top()==2); /// topmost element in the stack should now be 2
101-
102-
s.pop(); /// remove the topmost element from the stack
103-
assert(s.top()==1);
104-
105-
s.push(5); /// insert an element into the stack
106-
assert(s.top()==5); /// topmost element in the stack should now be 5
107-
108-
s.pop(); /// remove the topmost element from the stack
109-
assert(s.top()==1); /// topmost element in the stack should now be 1
110-
111-
assert(s.size()==1); /// size should be 1
84+
s.push(1); /// insert an element into the stack
85+
s.push(2); /// insert an element into the stack
86+
s.push(3); /// insert an element into the stack
87+
88+
assert(s.size() == 3); /// size should be 3
89+
90+
assert(s.top() == 3); /// topmost element in the stack should be 3
91+
92+
s.pop(); /// remove the topmost element from the stack
93+
assert(s.top() == 2); /// topmost element in the stack should now be 2
94+
95+
s.pop(); /// remove the topmost element from the stack
96+
assert(s.top() == 1);
97+
98+
s.push(5); /// insert an element into the stack
99+
assert(s.top() == 5); /// topmost element in the stack should now be 5
100+
101+
s.pop(); /// remove the topmost element from the stack
102+
assert(s.top() == 1); /// topmost element in the stack should now be 1
103+
104+
assert(s.size() == 1); /// size should be 1
112105
}
113106

114107
/**
@@ -119,8 +112,7 @@ static void test()
119112
* declared above.
120113
* @returns 0 on exit
121114
*/
122-
int main()
123-
{
115+
int main() {
124116
test(); // run self-test implementations
125117
return 0;
126118
}

math/area.cpp

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/**
22
* @file
3-
* @brief Implementations for the [area](https://en.wikipedia.org/wiki/Area) of various shapes
4-
* @details The area of a shape is the amount of 2D space it takes up.
5-
* All shapes have a formula to get the area of any given shape.
3+
* @brief Implementations for the [area](https://en.wikipedia.org/wiki/Area) of
4+
* various shapes
5+
* @details The area of a shape is the amount of 2D space it takes up.
6+
* All shapes have a formula to get the area of any given shape.
67
* These implementations support multiple return types.
7-
*
8+
*
89
* @author [Focusucof](https://github.com/Focusucof)
910
*/
1011
#define _USE_MATH_DEFINES
12+
#include <cassert> /// for assert
1113
#include <cmath> /// for M_PI definition and pow()
14+
#include <cmath>
1215
#include <cstdint> /// for uint16_t datatype
13-
#include <iostream> /// for IO operations
14-
#include <cassert> /// for assert
16+
#include <iostream> /// for IO operations
1517

1618
/**
1719
* @namespace math
@@ -115,25 +117,25 @@ T cylinder_surface_area(T radius, T height) {
115117
*/
116118
static void test() {
117119
// I/O variables for testing
118-
uint16_t int_length; // 16 bit integer length input
119-
uint16_t int_width; // 16 bit integer width input
120-
uint16_t int_base; // 16 bit integer base input
121-
uint16_t int_height; // 16 bit integer height input
122-
uint16_t int_expected; // 16 bit integer expected output
123-
uint16_t int_area; // 16 bit integer output
124-
125-
float float_length; // float length input
126-
float float_expected; // float expected output
127-
float float_area; // float output
128-
129-
double double_length; // double length input
130-
double double_width; // double width input
131-
double double_radius; // double radius input
132-
double double_height; // double height input
133-
double double_expected; // double expected output
134-
double double_area; // double output
135-
136-
// 1st test
120+
uint16_t int_length = 0; // 16 bit integer length input
121+
uint16_t int_width = 0; // 16 bit integer width input
122+
uint16_t int_base = 0; // 16 bit integer base input
123+
uint16_t int_height = 0; // 16 bit integer height input
124+
uint16_t int_expected = 0; // 16 bit integer expected output
125+
uint16_t int_area = 0; // 16 bit integer output
126+
127+
float float_length = NAN; // float length input
128+
float float_expected = NAN; // float expected output
129+
float float_area = NAN; // float output
130+
131+
double double_length = NAN; // double length input
132+
double double_width = NAN; // double width input
133+
double double_radius = NAN; // double radius input
134+
double double_height = NAN; // double height input
135+
double double_expected = NAN; // double expected output
136+
double double_area = NAN; // double output
137+
138+
// 1st test
137139
int_length = 5;
138140
int_expected = 25;
139141
int_area = math::square_area(int_length);
@@ -201,7 +203,9 @@ static void test() {
201203

202204
// 6th test
203205
double_radius = 6;
204-
double_expected = 113.09733552923255; // rounded down because the double datatype truncates after 14 decimal places
206+
double_expected =
207+
113.09733552923255; // rounded down because the double datatype
208+
// truncates after 14 decimal places
205209
double_area = math::circle_area(double_radius);
206210

207211
std::cout << "AREA OF A CIRCLE" << std::endl;
@@ -239,7 +243,8 @@ static void test() {
239243

240244
// 9th test
241245
double_radius = 10.0;
242-
double_expected = 1256.6370614359172; // rounded down because the whole value gets truncated
246+
double_expected = 1256.6370614359172; // rounded down because the whole
247+
// value gets truncated
243248
double_area = math::sphere_surface_area(double_radius);
244249

245250
std::cout << "SURFACE AREA OF A SPHERE" << std::endl;

0 commit comments

Comments
 (0)