Skip to content

Commit fd642d6

Browse files
committed
Now code is applicable for generic data types
1 parent e89b5bf commit fd642d6

File tree

1 file changed

+77
-49
lines changed

1 file changed

+77
-49
lines changed

sorting/bubble_sort_optimized.cpp

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* @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.
44
*
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.
98
*
109
* Additionally, it contains a set of self-tests to verify correctness
1110
* using the C++ standard library's `assert` mechanism.
@@ -15,78 +14,107 @@
1514
* std::vector<int> nums = {5, 4, 3, 2, 1};
1615
* sorting::bubble_sort(nums);
1716
* // 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"}
1821
* @endcode
1922
*
2023
* @author Rohith A K
21-
* @date 2025-09-25
24+
* @date 2025-09-30
2225
*/
2326

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
2832

2933
namespace sorting {
3034

3135
/**
32-
* @brief Sorts the given vector using the bubble sort algorithm.
36+
* @brief Sorts the given container using the bubble sort algorithm.
3337
*
3438
* 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.
3640
*
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
3944
*/
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;
5158
}
52-
}
53-
return nums;
59+
return cont;
5460
}
5561

5662
} // namespace sorting
5763

5864
/**
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.
6566
*/
6667
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);
7080

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);
7485

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);
7890

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";
80112
}
81113

82114
/**
83115
* @brief Main function
84-
*
85-
* Executes the self-test suite for bubble_sort.
86-
*
87-
* @returns 0 on successful execution
88116
*/
89117
int main() {
90-
tests(); // run self-test implementations
91-
return 0;
118+
tests();
119+
return 0;
92120
}

0 commit comments

Comments
 (0)