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