|
1 | 1 | /**
|
2 | 2 | * @file
|
3 | 3 | * @author [Aditya Prakash](https://adityaprakash.tech)
|
4 |
| - * @brief This is an implementation of a recursive version of the [Bubble sort algorithm](https://www.geeksforgeeks.org/recursive-bubble-sort/) |
| 4 | + * @brief This is an implementation of a recursive version of the [Bubble sort |
| 5 | + algorithm](https://www.geeksforgeeks.org/recursive-bubble-sort/) |
5 | 6 | *
|
6 | 7 | * @details
|
7 | 8 | * The working principle of the Bubble sort algorithm.
|
8 | 9 |
|
9 |
| - * Bubble sort is a simple sorting algorithm used to rearrange a set of ascending or descending order elements. |
10 |
| - * Bubble sort gets its name from the fact that data "bubbles" to the top of the dataset. |
11 |
| - |
| 10 | + * Bubble sort is a simple sorting algorithm used to rearrange a set of |
| 11 | + ascending or descending order elements. |
| 12 | + * Bubble sort gets its name from the fact that data "bubbles" to the top of the |
| 13 | + dataset. |
| 14 | +
|
12 | 15 | * ### Algorithm
|
13 | 16 |
|
14 | 17 | * What is Swap?
|
15 | 18 |
|
16 | 19 | * Swapping two numbers means that we interchange their values.
|
17 |
| - * Often, an additional variable is required for this operation. |
| 20 | + * Often, an additional variable is required for this operation. |
18 | 21 | * This is further illustrated in the following:
|
19 | 22 |
|
20 | 23 | * void swap(int x, int y){
|
|
26 | 29 | * The above process is a typical displacement process.
|
27 | 30 | * When we assign a value to x, the old value of x is lost.
|
28 | 31 | * That's why we create a temporary variable z to store the initial value of x.
|
29 |
| - * z is further used to assign the initial value of x to y, to complete swapping. |
| 32 | + * z is further used to assign the initial value of x to y, to complete |
| 33 | + swapping. |
30 | 34 |
|
31 | 35 | * Recursion
|
32 | 36 |
|
33 |
| - * While the recursive method does not necessarily have advantages over iterative |
| 37 | + * While the recursive method does not necessarily have advantages over |
| 38 | + iterative |
34 | 39 | * versions, but it is useful to enhance the understanding of the algorithm and
|
35 | 40 | * recursion itself. In Recursive Bubble sort algorithm, we firstly call the
|
36 |
| - * function on the entire array, and for every subsequent function call, we exclude |
37 |
| - * the last element. This fixes the last element for that sub-array.Formally, for |
| 41 | + * function on the entire array, and for every subsequent function call, we |
| 42 | + exclude |
| 43 | + * the last element. This fixes the last element for that sub-array.Formally, |
| 44 | + for |
38 | 45 | * `ith` iteration, we consider elements up to n-i, where n is the number of
|
39 | 46 | * elements in the array. Exit condition: n==1; i.e. the sub-array contains only
|
40 | 47 | * one element.
|
|
43 | 50 | * Time complexity: O(n) best case; O(n²) average case; O(n²) worst case
|
44 | 51 | * Space complexity: O(n)
|
45 | 52 |
|
46 |
| - * We need to traverse the array `n * (n-1)` times. However, if the entire array is |
47 |
| - * already sorted, then we need to traverse it only once. Hence, O(n) is the best case |
| 53 | + * We need to traverse the array `n * (n-1)` times. However, if the entire array |
| 54 | + is |
| 55 | + * already sorted, then we need to traverse it only once. Hence, O(n) is the |
| 56 | + best case |
48 | 57 | * complexity
|
49 | 58 | */
|
50 | 59 |
|
51 |
| -#include <cassert> /// for assert |
52 |
| -#include <iostream> /// for IO operations |
53 |
| -#include <vector> /// for std::vector |
54 |
| -#include <array> /// for std::array |
55 |
| -#include <algorithm> /// for std::is_sorted |
| 60 | +#include <algorithm> /// for std::is_sorted |
| 61 | +#include <array> /// for std::array |
| 62 | +#include <cassert> /// for assert |
| 63 | +#include <cstdint> /// for integral typedefs |
| 64 | +#include <iostream> /// for IO operations |
| 65 | +#include <vector> /// for std::vector |
56 | 66 |
|
57 | 67 | /**
|
58 | 68 | * @namespace sorting
|
|
61 | 71 | namespace sorting {
|
62 | 72 |
|
63 | 73 | /**
|
64 |
| - * @brief This is an implementation of the recursive_bubble_sort. A vector is passed |
65 |
| - * to the function which is then dereferenced, so that the changes are |
| 74 | + * @brief This is an implementation of the recursive_bubble_sort. A vector is |
| 75 | + * passed to the function which is then dereferenced, so that the changes are |
66 | 76 | * reflected in the original vector. It also accepts a second parameter of
|
67 | 77 | * type `int` and name `n`, which is the size of the array.
|
68 |
| - * |
| 78 | + * |
69 | 79 | * @tparam T type of data variables in the array
|
70 | 80 | * @param nums our array of elements.
|
71 | 81 | * @param n size of the array
|
@@ -136,14 +146,13 @@ static void test() {
|
136 | 146 | std::cout << double_arr[i] << ", ";
|
137 | 147 | }
|
138 | 148 | std::cout << std::endl;
|
139 |
| - |
140 | 149 | }
|
141 | 150 |
|
142 | 151 | /**
|
143 | 152 | * @brief Main function
|
144 | 153 | * @returns 0 on exit
|
145 | 154 | */
|
146 |
| -int main() { |
| 155 | +int main() { |
147 | 156 | test(); // run self-test implementations
|
148 | 157 | return 0;
|
149 | 158 | }
|
0 commit comments