Skip to content

Commit 097d861

Browse files
committed
added file header documentation and clang format is followed
1 parent 5d04574 commit 097d861

File tree

1 file changed

+75
-45
lines changed

1 file changed

+75
-45
lines changed

sorting/bubble_sort_optimized.cpp

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,92 @@
1+
/**
2+
* @file bubble_sort.cpp
3+
* @brief Implementation of the Bubble Sort algorithm with self-tests.
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.
9+
*
10+
* Additionally, it contains a set of self-tests to verify correctness
11+
* using the C++ standard library's `assert` mechanism.
12+
*
13+
* Example:
14+
* @code
15+
* std::vector<int> nums = {5, 4, 3, 2, 1};
16+
* sorting::bubble_sort(nums);
17+
* // nums becomes {1, 2, 3, 4, 5}
18+
* @endcode
19+
*
20+
* @author Rohith A K[https://github.com/rohleet]
21+
* @date 2025-09-25
22+
*/
23+
24+
#include <cassert> /// for assert
125
#include <iostream> /// for IO operations
26+
#include <utility> /// for std::swap
227
#include <vector> /// for std::vector
3-
#include <cassert> /// for assert
428

529
namespace sorting {
630

7-
/**
8-
* @brief Sorts the given vector using the bubble sort algorithm.
9-
* Optimized by reducing the number of passes in average cases.
10-
* @param nums the vector of integers to be sorted
11-
* @return reference to the sorted vector
12-
*/
13-
std::vector<int>& bubble_sort(std::vector<int>& nums) {
14-
int flag=0;
15-
int temp;
16-
for(int i=0; i<nums.size()-1;i++){
17-
flag=0;
18-
for(int j=0; j<nums.size()-1-i;j++){
19-
if(nums[j]>nums[j+1]){
20-
temp=nums[j];
21-
nums[j]=nums[j+1];
22-
nums[j+1]=temp;
23-
flag=1;
24-
}
25-
}
26-
if(flag==0){
27-
break;
28-
}
29-
}
30-
return nums;
31+
/**
32+
* @brief Sorts the given vector using the bubble sort algorithm.
33+
*
34+
* This optimized bubble sort reduces the number of passes in average cases
35+
* by checking if the array is already sorted during each pass.
36+
*
37+
* @param nums the vector of integers to be sorted
38+
* @return reference to the sorted vector
39+
*/
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+
}
3148
}
49+
if (!swapped) {
50+
break;
51+
}
52+
}
53+
return nums;
54+
}
3255

33-
/**
34-
* @brief Self-test implementations
35-
* @returns void
36-
*/
37-
static void tests() {
38-
std::vector<int> v1 = {2,8,1,6,2,0,3,6};
39-
std::vector<int> expected1 = {0,1,2,2,3,6,6,8};
40-
assert(bubble_sort(v1) == expected1);
56+
/**
57+
* @brief Self-test implementations of the bubble_sort function.
58+
*
59+
* Runs a set of predefined test cases using `assert` to ensure
60+
* the correctness of the bubble_sort implementation.
61+
*
62+
* @returns void
63+
*/
64+
static void tests() {
65+
std::vector<int> v1 = {2, 8, 1, 6, 2, 0, 3, 6};
66+
const std::vector<int> expected1 = {0, 1, 2, 2, 3, 6, 6, 8};
67+
assert(bubble_sort(v1) == expected1);
4168

42-
std::vector<int> v2 = {5,4,3,2,1};
43-
std::vector<int> expected2 = {1,2,3,4,5};
44-
assert(bubble_sort(v2) == expected2);
69+
std::vector<int> v2 = {5, 4, 3, 2, 1};
70+
const std::vector<int> expected2 = {1, 2, 3, 4, 5};
71+
assert(bubble_sort(v2) == expected2);
4572

46-
std::vector<int> v3 = {1,2,3,4,5};
47-
std::vector<int> expected3 = {1,2,3,4,5};
48-
assert(bubble_sort(v3) == expected3);
73+
std::vector<int> v3 = {1, 2, 3, 4, 5};
74+
const std::vector<int> expected3 = {1, 2, 3, 4, 5};
75+
assert(bubble_sort(v3) == expected3);
4976

50-
std::cout << "All tests have successfully passed!\n";
51-
}
77+
std::cout << "All tests have successfully passed!\n";
5278
}
5379

80+
} // namespace sorting
81+
5482
/**
5583
* @brief Main function
56-
* @returns 0 on exit
84+
*
85+
* Executes the self-test suite for bubble_sort.
86+
*
87+
* @returns 0 on successful execution
5788
*/
5889
int main() {
59-
sorting::tests(); // run self-test implementations
60-
return 0;
90+
sorting::tests(); // run self-test implementations
91+
return 0;
6192
}
62-

0 commit comments

Comments
 (0)