|
1 | 1 | /**
|
2 | 2 | * @file bubble_sort.cpp
|
3 |
| - * @brief Implementation of the Bubble Sort algorithm with self-tests. |
| 3 | + * @brief Generic implementation of the Bubble Sort algorithm with self-tests. |
4 | 4 | *
|
5 |
| - * This file contains an implementation of the bubble sort algorithm |
6 |
| - * inside the `sorting` namespace. It includes an optimized version |
7 |
| - * of bubble sort that reduces the number of passes when the input |
8 |
| - * is already sorted or becomes sorted before completing all iterations. |
| 5 | + * This file contains a template-based implementation of the bubble sort algorithm |
| 6 | + * inside the `sorting` namespace. It supports sorting of any container |
| 7 | + * holding elements that can be compared using the `>` operator. |
9 | 8 | *
|
10 | 9 | * Additionally, it contains a set of self-tests to verify correctness
|
11 | 10 | * using the C++ standard library's `assert` mechanism.
|
|
15 | 14 | * std::vector<int> nums = {5, 4, 3, 2, 1};
|
16 | 15 | * sorting::bubble_sort(nums);
|
17 | 16 | * // nums becomes {1, 2, 3, 4, 5}
|
| 17 | + * |
| 18 | + * std::vector<std::string> words = {"apple", "banana", "cherry"}; |
| 19 | + * sorting::bubble_sort(words); |
| 20 | + * // words becomes {"apple", "banana", "cherry"} |
18 | 21 | * @endcode
|
19 | 22 | *
|
20 | 23 | * @author Rohith A K
|
21 |
| - * @date 2025-09-25 |
| 24 | + * @date 2025-09-30 |
22 | 25 | */
|
23 | 26 |
|
24 |
| -#include <cassert> /// for assert |
25 |
| -#include <iostream> /// for IO operations |
26 |
| -#include <utility> /// for std::swap |
27 |
| -#include <vector> /// for std::vector |
| 27 | +#include <cassert> ///< for assert |
| 28 | +#include <iostream> ///< for IO operations |
| 29 | +#include <utility> ///< for std::swap |
| 30 | +#include <vector> ///< for std::vector |
| 31 | +#include <string> ///< for std::string |
28 | 32 |
|
29 | 33 | namespace sorting {
|
30 | 34 |
|
31 | 35 | /**
|
32 |
| - * @brief Sorts the given vector using the bubble sort algorithm. |
| 36 | + * @brief Sorts the given container using the bubble sort algorithm. |
33 | 37 | *
|
34 | 38 | * This optimized bubble sort reduces the number of passes in average cases
|
35 |
| - * by checking if the array is already sorted during each pass. |
| 39 | + * by checking if the container is already sorted during each pass. |
36 | 40 | *
|
37 |
| - * @param nums the vector of integers to be sorted |
38 |
| - * @return reference to the sorted vector |
| 41 | + * @tparam Container Type of the container (e.g., std::vector<T>) |
| 42 | + * @param cont The container to be sorted |
| 43 | + * @return Reference to the sorted container |
39 | 44 | */
|
40 |
| -std::vector<int>& bubble_sort(std::vector<int>& nums) { |
41 |
| - for (std::size_t i = 0; i < nums.size() - 1; i++) { |
42 |
| - bool swapped = false; |
43 |
| - for (std::size_t j = 0; j < nums.size() - 1 - i; j++) { |
44 |
| - if (nums[j] > nums[j + 1]) { |
45 |
| - std::swap(nums[j], nums[j + 1]); |
46 |
| - swapped = true; |
47 |
| - } |
48 |
| - } |
49 |
| - if (!swapped) { |
50 |
| - break; |
| 45 | +template <typename Container> |
| 46 | +Container& bubble_sort(Container& cont) { |
| 47 | + if (cont.size() < 2) return cont; // handle empty or single-element case |
| 48 | + |
| 49 | + for (std::size_t i = 0; i < cont.size() - 1; ++i) { |
| 50 | + bool swapped = false; |
| 51 | + for (std::size_t j = 0; j < cont.size() - 1 - i; ++j) { |
| 52 | + if (cont[j] > cont[j + 1]) { |
| 53 | + std::swap(cont[j], cont[j + 1]); |
| 54 | + swapped = true; |
| 55 | + } |
| 56 | + } |
| 57 | + if (!swapped) break; |
51 | 58 | }
|
52 |
| - } |
53 |
| - return nums; |
| 59 | + return cont; |
54 | 60 | }
|
55 | 61 |
|
56 | 62 | } // namespace sorting
|
57 | 63 |
|
58 | 64 | /**
|
59 |
| - * @brief Self-test implementations of the bubble_sort function. |
60 |
| - * |
61 |
| - * Runs a set of predefined test cases using `assert` to ensure |
62 |
| - * the correctness of the bubble_sort implementation. |
63 |
| - * |
64 |
| - * @returns void |
| 65 | + * @brief Self-test implementations of the generic bubble_sort function. |
65 | 66 | */
|
66 | 67 | static void tests() {
|
67 |
| - std::vector<int> v1 = {2, 8, 1, 6, 2, 0, 3, 6}; |
68 |
| - const std::vector<int> expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; |
69 |
| - assert(sorting::bubble_sort(v1) == expected1); |
| 68 | + // Integer test cases |
| 69 | + std::vector<int> v1 = {2, 8, 1, 6, 2, 0, 3, 6}; |
| 70 | + const std::vector<int> expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; |
| 71 | + assert(sorting::bubble_sort(v1) == expected1); |
| 72 | + |
| 73 | + std::vector<int> v2 = {5, 4, 3, 2, 1}; |
| 74 | + const std::vector<int> expected2 = {1, 2, 3, 4, 5}; |
| 75 | + assert(sorting::bubble_sort(v2) == expected2); |
| 76 | + |
| 77 | + std::vector<int> v3 = {1, 2, 3, 4, 5}; |
| 78 | + const std::vector<int> expected3 = {1, 2, 3, 4, 5}; |
| 79 | + assert(sorting::bubble_sort(v3) == expected3); |
70 | 80 |
|
71 |
| - std::vector<int> v2 = {5, 4, 3, 2, 1}; |
72 |
| - const std::vector<int> expected2 = {1, 2, 3, 4, 5}; |
73 |
| - assert(sorting::bubble_sort(v2) == expected2); |
| 81 | + // Negative numbers |
| 82 | + std::vector<int> v4 = {-5, -1, -3, -2, -4}; |
| 83 | + const std::vector<int> expected4 = {-5, -4, -3, -2, -1}; |
| 84 | + assert(sorting::bubble_sort(v4) == expected4); |
74 | 85 |
|
75 |
| - std::vector<int> v3 = {1, 2, 3, 4, 5}; |
76 |
| - const std::vector<int> expected3 = {1, 2, 3, 4, 5}; |
77 |
| - assert(sorting::bubble_sort(v3) == expected3); |
| 86 | + // Mixed negative and positive |
| 87 | + std::vector<int> v5 = {-1, 0, 1, -2, 2}; |
| 88 | + const std::vector<int> expected5 = {-2, -1, 0, 1, 2}; |
| 89 | + assert(sorting::bubble_sort(v5) == expected5); |
78 | 90 |
|
79 |
| - std::cout << "All tests have successfully passed!\n"; |
| 91 | + // Empty vector |
| 92 | + std::vector<int> v6 = {}; |
| 93 | + const std::vector<int> expected6 = {}; |
| 94 | + assert(sorting::bubble_sort(v6) == expected6); |
| 95 | + |
| 96 | + // Double test case |
| 97 | + std::vector<double> vd = {3.5, 2.1, 4.8, 1.2}; |
| 98 | + const std::vector<double> expectedd = {1.2, 2.1, 3.5, 4.8}; |
| 99 | + assert(sorting::bubble_sort(vd) == expectedd); |
| 100 | + |
| 101 | + // String test case |
| 102 | + std::vector<std::string> vs = {"apple", "banana", "cherry", "apricot"}; |
| 103 | + const std::vector<std::string> expecteds = {"apple", "apricot", "banana", "cherry"}; |
| 104 | + assert(sorting::bubble_sort(vs) == expecteds); |
| 105 | + |
| 106 | + // Char test case |
| 107 | + std::vector<char> vc = {'z', 'b', 'a', 'd', 'c'}; |
| 108 | + const std::vector<char> expectedc = {'a', 'b', 'c', 'd', 'z'}; |
| 109 | + assert(sorting::bubble_sort(vc) == expectedc); |
| 110 | + |
| 111 | + std::cout << "All generic tests have successfully passed!\n"; |
80 | 112 | }
|
81 | 113 |
|
82 | 114 | /**
|
83 | 115 | * @brief Main function
|
84 |
| - * |
85 |
| - * Executes the self-test suite for bubble_sort. |
86 |
| - * |
87 |
| - * @returns 0 on successful execution |
88 | 116 | */
|
89 | 117 | int main() {
|
90 |
| - tests(); // run self-test implementations |
91 |
| - return 0; |
| 118 | + tests(); |
| 119 | + return 0; |
92 | 120 | }
|
0 commit comments