|
| 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 |
1 | 25 | #include <iostream> /// for IO operations
|
| 26 | +#include <utility> /// for std::swap |
2 | 27 | #include <vector> /// for std::vector
|
3 |
| -#include <cassert> /// for assert |
4 | 28 |
|
5 | 29 | namespace sorting {
|
6 | 30 |
|
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 | + } |
31 | 48 | }
|
| 49 | + if (!swapped) { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + return nums; |
| 54 | +} |
32 | 55 |
|
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); |
41 | 68 |
|
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); |
45 | 72 |
|
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); |
49 | 76 |
|
50 |
| - std::cout << "All tests have successfully passed!\n"; |
51 |
| - } |
| 77 | + std::cout << "All tests have successfully passed!\n"; |
52 | 78 | }
|
53 | 79 |
|
| 80 | +} // namespace sorting |
| 81 | + |
54 | 82 | /**
|
55 | 83 | * @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 |
57 | 88 | */
|
58 | 89 | int main() {
|
59 |
| - sorting::tests(); // run self-test implementations |
60 |
| - return 0; |
| 90 | + sorting::tests(); // run self-test implementations |
| 91 | + return 0; |
61 | 92 | }
|
62 |
| - |
|
0 commit comments