Skip to content

Commit d9113db

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 47685a5 + 512efd1 commit d9113db

File tree

2 files changed

+192
-43
lines changed

2 files changed

+192
-43
lines changed

math/fibonacci.cpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
/**
22
* @file
3-
* @brief Generate fibonacci sequence
3+
* @brief n-th [Fibonacci
4+
* number](https://en.wikipedia.org/wiki/Fibonacci_sequence).
45
*
5-
* Calculate the the value on Fibonacci's sequence given an
6-
* integer as input.
6+
* @details
7+
* Naive recursive implementation to calculate the n-th Fibonacci number.
78
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
89
*
910
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
1011
*/
11-
#include <cassert>
12-
#include <iostream>
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for IO operations
1314

1415
/**
15-
* Recursively compute sequences
16-
* @param n input
17-
* @returns n-th element of the Fbinacci's sequence
16+
* @namespace math
17+
* @brief Math algorithms
18+
*/
19+
namespace math {
20+
/**
21+
* @namespace fibonacci
22+
* @brief Functions for Fibonacci sequence
23+
*/
24+
namespace fibonacci {
25+
/**
26+
* @brief Function to compute the n-th Fibonacci number
27+
* @param n the index of the Fibonacci number
28+
* @returns n-th element of the Fibonacci's sequence
1829
*/
1930
uint64_t fibonacci(uint64_t n) {
20-
/* If the input is 0 or 1 just return the same
21-
This will set the first 2 values of the sequence */
31+
// If the input is 0 or 1 just return the same (Base Case)
32+
// This will set the first 2 values of the sequence
2233
if (n <= 1) {
2334
return n;
2435
}
2536

26-
/* Add the last 2 values of the sequence to get next */
37+
// Add the preceding 2 values of the sequence to get next
2738
return fibonacci(n - 1) + fibonacci(n - 2);
2839
}
40+
} // namespace fibonacci
41+
} // namespace math
2942

3043
/**
31-
* Function for testing the fibonacci() function with a few
32-
* test cases and assert statement.
44+
* @brief Self-test implementation
3345
* @returns `void`
34-
*/
46+
*/
3547
static void test() {
36-
uint64_t test_case_1 = fibonacci(0);
37-
assert(test_case_1 == 0);
38-
std::cout << "Passed Test 1!" << std::endl;
39-
40-
uint64_t test_case_2 = fibonacci(1);
41-
assert(test_case_2 == 1);
42-
std::cout << "Passed Test 2!" << std::endl;
43-
44-
uint64_t test_case_3 = fibonacci(2);
45-
assert(test_case_3 == 1);
46-
std::cout << "Passed Test 3!" << std::endl;
47-
48-
uint64_t test_case_4 = fibonacci(3);
49-
assert(test_case_4 == 2);
50-
std::cout << "Passed Test 4!" << std::endl;
51-
52-
uint64_t test_case_5 = fibonacci(4);
53-
assert(test_case_5 == 3);
54-
std::cout << "Passed Test 5!" << std::endl;
55-
56-
uint64_t test_case_6 = fibonacci(15);
57-
assert(test_case_6 == 610);
58-
std::cout << "Passed Test 6!" << std::endl << std::endl;
48+
assert(math::fibonacci::fibonacci(0) == 0);
49+
assert(math::fibonacci::fibonacci(1) == 1);
50+
assert(math::fibonacci::fibonacci(2) == 1);
51+
assert(math::fibonacci::fibonacci(3) == 2);
52+
assert(math::fibonacci::fibonacci(4) == 3);
53+
assert(math::fibonacci::fibonacci(15) == 610);
54+
assert(math::fibonacci::fibonacci(20) == 6765);
55+
std::cout << "All tests have passed successfully!\n";
5956
}
6057

61-
/// Main function
58+
/**
59+
* @brief Main function
60+
* @returns 0 on exit
61+
*/
6262
int main() {
63-
test();
64-
int n = 0;
65-
std::cin >> n;
66-
assert(n >= 0);
67-
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
63+
test(); // run self-test implementations
64+
return 0;
6865
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* @file
3+
* @brief Insertion Sort Algorithm
4+
* @author [Dhanush S](https://github.com/Fandroid745)
5+
*
6+
* @details
7+
* Insertion sort is a simple sorting algorithm that builds the final
8+
* sorted array one element at a time. It is much less efficient compared
9+
* to other sorting algorithms like heap sort, merge sort, or quick sort.
10+
*
11+
* However, it has several advantages:
12+
* - Easy to implement.
13+
* - Efficient for small data sets.
14+
* - More efficient than other O(n²) algorithms like selection sort or bubble sort.
15+
* - Stable: it does not change the relative order of elements with equal keys.
16+
*
17+
* Insertion sort works similarly to how people sort playing cards in their hands.
18+
* The algorithm iterates through the list and inserts each element into its correct
19+
* position in the sorted portion of the array.
20+
*
21+
* The time complexity of the algorithm is \f$O(n^2)\f$, and in some cases, it
22+
* can be \f$O(n)\f$.
23+
*
24+
* Example execution:
25+
* 1. Start with the array [4, 3, 2, 5, 1].
26+
* 2. Insert 3 in its correct position: [3, 4, 2, 5, 1].
27+
* 3. Insert 2: [2, 3, 4, 5, 1].
28+
* 4. Continue this until the array is sorted: [1, 2, 3, 4, 5].
29+
*/
30+
31+
32+
#include <algorithm> /// for std::is_sorted
33+
#include <cassert> /// for assert function in testing
34+
#include <iostream> /// for std::cout and std::endl
35+
#include <vector> /// for using std::vector
36+
37+
/**
38+
* @namespace sorting
39+
* @brief Contains sorting algorithms
40+
*/
41+
namespace sorting {
42+
43+
/**
44+
* @brief Insertion Sort Function
45+
*
46+
* @tparam T Type of the array elements
47+
* @param[in,out] arr Array to be sorted
48+
* @param n Size of the array
49+
*/
50+
template <typename T>
51+
void insertionSort(T *arr, int n) {
52+
for (int i = 1; i < n; i++) {
53+
T temp = arr[i];
54+
int j = i - 1;
55+
while (j >= 0 && temp < arr[j]) {
56+
arr[j + 1] = arr[j];
57+
j--;
58+
}
59+
arr[j + 1] = temp;
60+
}
61+
}
62+
63+
/**
64+
* @brief Insertion Sort for a vector
65+
*
66+
* @tparam T Type of the vector elements
67+
* @param [in,out] arr Pointer to the vector to be sorted
68+
*/
69+
template <typename T>
70+
void insertionSort(std::vector<T> *arr) {
71+
size_t n = arr->size();
72+
73+
for (size_t i = 1; i < n; i++) {
74+
T temp = arr->at(i);
75+
int32_t j = i - 1;
76+
while (j >= 0 && temp < arr->at(j)) {
77+
arr->at(j + 1) = arr->at(j);
78+
j--;
79+
}
80+
arr->at(j + 1) = temp;
81+
}
82+
}
83+
84+
} // namespace sorting
85+
86+
/**
87+
* @brief Helper function to create a random array
88+
*
89+
* @tparam T Type of the array elements
90+
* @param arr Array to fill (must be pre-allocated)
91+
* @param N Number of elements in the array
92+
*/
93+
template <typename T>
94+
static void create_random_array(T *arr, int N) {
95+
while (N--) {
96+
double r = (std::rand() % 10000 - 5000) / 100.f;
97+
arr[N] = static_cast<T>(r);
98+
}
99+
}
100+
101+
/**
102+
* @brief self test implementation
103+
* @return void
104+
*/
105+
static void tests() {
106+
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
107+
std::cout << "Test 1... ";
108+
sorting::insertionSort(arr1, 10);
109+
assert(std::is_sorted(arr1, arr1 + 10));
110+
std::cout << "passed" << std::endl;
111+
112+
int arr2[5] = {5, -3, 7, -2, 1};
113+
std::cout << "Test 2... ";
114+
sorting::insertionSort(arr2, 5);
115+
assert(std::is_sorted(arr2, arr2 + 5));
116+
std::cout << "passed" << std::endl;
117+
118+
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
119+
std::cout << "Test 3... ";
120+
sorting::insertionSort(arr3, 5);
121+
assert(std::is_sorted(arr3, arr3 + 5));
122+
std::cout << "passed" << std::endl;
123+
124+
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
125+
std::cout << "Test 4... ";
126+
sorting::insertionSort(&arr4);
127+
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
128+
std::cout << "passed" << std::endl;
129+
130+
int arr5[50];
131+
std::cout << "Test 5... ";
132+
create_random_array(arr5, 50);
133+
sorting::insertionSort(arr5, 50);
134+
assert(std::is_sorted(arr5, arr5 + 50));
135+
std::cout << "passed" << std::endl;
136+
137+
float arr6[50];
138+
std::cout << "Test 6... ";
139+
create_random_array(arr6, 50);
140+
sorting::insertionSort(arr6, 50);
141+
assert(std::is_sorted(arr6, arr6 + 50));
142+
std::cout << "passed" << std::endl;
143+
}
144+
145+
/**
146+
* @brief Main function
147+
* @return 0 on successful exit.
148+
*/
149+
int main() {
150+
tests(); /// run self test implementations
151+
return 0;
152+
}

0 commit comments

Comments
 (0)