|
| 1 | +/** |
| 2 | + * @file |
| 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/) |
| 5 | + * |
| 6 | + * @details |
| 7 | + * The working principle of the Bubble sort algorithm. |
| 8 | +
|
| 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 | + |
| 12 | + * ### Algorithm |
| 13 | +
|
| 14 | + * What is Swap? |
| 15 | +
|
| 16 | + * Swapping two numbers means that we interchange their values. |
| 17 | + * Often, an additional variable is required for this operation. |
| 18 | + * This is further illustrated in the following: |
| 19 | +
|
| 20 | + * void swap(int x, int y){ |
| 21 | + * int z = x; |
| 22 | + * x = y; |
| 23 | + * y = z; |
| 24 | + * } |
| 25 | +
|
| 26 | + * The above process is a typical displacement process. |
| 27 | + * When we assign a value to x, the old value of x is lost. |
| 28 | + * 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. |
| 30 | +
|
| 31 | + * Recursion |
| 32 | +
|
| 33 | + * While the recursive method does not necessarily have advantages over iterative |
| 34 | + * versions, but it is useful to enhance the understanding of the algorithm and |
| 35 | + * 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 |
| 38 | + * `ith` iteration, we consider elements up to n-i, where n is the number of |
| 39 | + * elements in the array. Exit condition: n==1; i.e. the sub-array contains only |
| 40 | + * one element. |
| 41 | +
|
| 42 | + * Complexity |
| 43 | + * Time complexity: O(n) best case; O(n²) average case; O(n²) worst case |
| 44 | + * Space complexity: O(n) |
| 45 | +
|
| 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 |
| 48 | + * complexity |
| 49 | +*/ |
| 50 | + |
| 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 |
| 56 | + |
| 57 | +/** |
| 58 | + * @namespace sorting |
| 59 | + * @brief Sorting algorithms |
| 60 | + */ |
| 61 | +namespace sorting { |
| 62 | + |
| 63 | +/** |
| 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 |
| 66 | + * reflected in the original vector. It also accepts a second parameter of |
| 67 | + * type `int` and name `n`, which is the size of the array. |
| 68 | + * |
| 69 | + * @tparam T type of data variables in the array |
| 70 | + * @param nums our array of elements. |
| 71 | + * @param n size of the array |
| 72 | + */ |
| 73 | +template <typename T> |
| 74 | +void recursive_bubble_sort(std::vector<T> *nums, uint64_t n) { |
| 75 | + if (n == 1) { //!< base case; when size of the array is 1 |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + for (uint64_t i = 0; i < n - 1; i++) { //!< iterating over the entire array |
| 80 | + //!< if a larger number appears before the smaller one, swap them. |
| 81 | + if ((*nums)[i] > (*nums)[i + 1]) { |
| 82 | + std::swap((*nums)[i], (*nums)[i + 1]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + //!< calling the function after we have fixed the last element |
| 87 | + recursive_bubble_sort(nums, n - 1); |
| 88 | +} |
| 89 | +} // namespace sorting |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Self-test implementations |
| 93 | + * @returns void |
| 94 | + */ |
| 95 | +static void test() { |
| 96 | + // 1st example. Creating an array of type `int`. |
| 97 | + std::cout << "1st test using `int`\n"; |
| 98 | + const uint64_t size = 6; |
| 99 | + std::vector<int64_t> arr; |
| 100 | + // populating the array |
| 101 | + arr.push_back(22); |
| 102 | + arr.push_back(46); |
| 103 | + arr.push_back(94); |
| 104 | + arr.push_back(12); |
| 105 | + arr.push_back(37); |
| 106 | + arr.push_back(63); |
| 107 | + // array populating ends |
| 108 | + |
| 109 | + sorting::recursive_bubble_sort(&arr, size); |
| 110 | + assert(std::is_sorted(std::begin(arr), std::end(arr))); |
| 111 | + std::cout << " 1st test passed!\n"; |
| 112 | + // printing the array |
| 113 | + for (uint64_t i = 0; i < size; i++) { |
| 114 | + std::cout << arr[i] << ", "; |
| 115 | + } |
| 116 | + std::cout << std::endl; |
| 117 | + |
| 118 | + // 2nd example. Creating an array of type `double`. |
| 119 | + std::cout << "2nd test using doubles\n"; |
| 120 | + std::vector<double> double_arr; |
| 121 | + |
| 122 | + // populating the array |
| 123 | + double_arr.push_back(20.4); |
| 124 | + double_arr.push_back(62.7); |
| 125 | + double_arr.push_back(12.2); |
| 126 | + double_arr.push_back(43.6); |
| 127 | + double_arr.push_back(74.1); |
| 128 | + double_arr.push_back(57.9); |
| 129 | + // array populating ends |
| 130 | + |
| 131 | + sorting::recursive_bubble_sort(&double_arr, size); |
| 132 | + assert(std::is_sorted(std::begin(double_arr), std::end(double_arr))); |
| 133 | + std::cout << " 2nd test passed!\n"; |
| 134 | + // printing the array |
| 135 | + for (uint64_t i = 0; i < size; i++) { |
| 136 | + std::cout << double_arr[i] << ", "; |
| 137 | + } |
| 138 | + std::cout << std::endl; |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * @brief Main function |
| 144 | + * @returns 0 on exit |
| 145 | + */ |
| 146 | +int main() { |
| 147 | + test(); // run self-test implementations |
| 148 | + return 0; |
| 149 | +} |
0 commit comments