From 302b33e34e9ff3f1f22a28ce20c2f286b124b0cf Mon Sep 17 00:00:00 2001 From: Rohith A K Date: Wed, 24 Sep 2025 01:44:07 +0530 Subject: [PATCH 1/5] Added an optimized bubble sort in sorts --- sorting/bubble_sort_optimized.cpp | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sorting/bubble_sort_optimized.cpp diff --git a/sorting/bubble_sort_optimized.cpp b/sorting/bubble_sort_optimized.cpp new file mode 100644 index 00000000000..0f773f2404c --- /dev/null +++ b/sorting/bubble_sort_optimized.cpp @@ -0,0 +1,37 @@ + +/*This version of bubble sort will reduce the number of passes needed if average cases and also the number od swaps needed in each pass which optimize the algorithm*/ + + +#include +#include +using namespace std; + +vector& bubble_sort(vector& nums){ + int flag=0; + int temp; + for(int i=0; inums[j+1]){ + temp=nums[j]; + nums[j]=nums[j+1]; + nums[j+1]=temp; + flag=1; + } + } + if(flag==0){ + break; + } + } + return nums; +} + + +int main() { + vector v={2,8,1,6,2,0,3,6}; + v=bubble_sort(v); + for(int i=0;i Date: Wed, 24 Sep 2025 14:57:54 +0530 Subject: [PATCH 2/5] Added necessary documentation and removed the using namespace --- sorting/bubble_sort_optimized.cpp | 81 ++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/sorting/bubble_sort_optimized.cpp b/sorting/bubble_sort_optimized.cpp index 0f773f2404c..5174c024b4c 100644 --- a/sorting/bubble_sort_optimized.cpp +++ b/sorting/bubble_sort_optimized.cpp @@ -1,37 +1,62 @@ +#include /// for IO operations +#include /// for std::vector +#include /// for assert -/*This version of bubble sort will reduce the number of passes needed if average cases and also the number od swaps needed in each pass which optimize the algorithm*/ +namespace sorting { + /** + * @brief Sorts the given vector using the bubble sort algorithm. + * Optimized by reducing the number of passes in average cases. + * @param nums the vector of integers to be sorted + * @return reference to the sorted vector + */ + std::vector& bubble_sort(std::vector& nums) { + int flag=0; + int temp; + for(int i=0; inums[j+1]){ + temp=nums[j]; + nums[j]=nums[j+1]; + nums[j+1]=temp; + flag=1; + } + } + if(flag==0){ + break; + } + } + return nums; + } -#include -#include -using namespace std; + /** + * @brief Self-test implementations + * @returns void + */ + static void tests() { + std::vector v1 = {2,8,1,6,2,0,3,6}; + std::vector expected1 = {0,1,2,2,3,6,6,8}; + assert(bubble_sort(v1) == expected1); -vector& bubble_sort(vector& nums){ - int flag=0; - int temp; - for(int i=0; inums[j+1]){ - temp=nums[j]; - nums[j]=nums[j+1]; - nums[j+1]=temp; - flag=1; - } - } - if(flag==0){ - break; - } + std::vector v2 = {5,4,3,2,1}; + std::vector expected2 = {1,2,3,4,5}; + assert(bubble_sort(v2) == expected2); + + std::vector v3 = {1,2,3,4,5}; + std::vector expected3 = {1,2,3,4,5}; + assert(bubble_sort(v3) == expected3); + + std::cout << "All tests have successfully passed!\n"; } - return nums; } - +/** + * @brief Main function + * @returns 0 on exit + */ int main() { - vector v={2,8,1,6,2,0,3,6}; - v=bubble_sort(v); - for(int i=0;i Date: Thu, 25 Sep 2025 18:55:55 +0530 Subject: [PATCH 3/5] added file header documentation and clang format is followed --- sorting/bubble_sort_optimized.cpp | 120 +++++++++++++++++++----------- 1 file changed, 75 insertions(+), 45 deletions(-) diff --git a/sorting/bubble_sort_optimized.cpp b/sorting/bubble_sort_optimized.cpp index 5174c024b4c..1a0fca50b4b 100644 --- a/sorting/bubble_sort_optimized.cpp +++ b/sorting/bubble_sort_optimized.cpp @@ -1,62 +1,92 @@ +/** + * @file bubble_sort.cpp + * @brief Implementation of the Bubble Sort algorithm with self-tests. + * + * This file contains an implementation of the bubble sort algorithm + * inside the `sorting` namespace. It includes an optimized version + * of bubble sort that reduces the number of passes when the input + * is already sorted or becomes sorted before completing all iterations. + * + * Additionally, it contains a set of self-tests to verify correctness + * using the C++ standard library's `assert` mechanism. + * + * Example: + * @code + * std::vector nums = {5, 4, 3, 2, 1}; + * sorting::bubble_sort(nums); + * // nums becomes {1, 2, 3, 4, 5} + * @endcode + * + * @author Rohith A K[https://github.com/rohleet] + * @date 2025-09-25 + */ + +#include /// for assert #include /// for IO operations +#include /// for std::swap #include /// for std::vector -#include /// for assert namespace sorting { - /** - * @brief Sorts the given vector using the bubble sort algorithm. - * Optimized by reducing the number of passes in average cases. - * @param nums the vector of integers to be sorted - * @return reference to the sorted vector - */ - std::vector& bubble_sort(std::vector& nums) { - int flag=0; - int temp; - for(int i=0; inums[j+1]){ - temp=nums[j]; - nums[j]=nums[j+1]; - nums[j+1]=temp; - flag=1; - } - } - if(flag==0){ - break; - } - } - return nums; +/** + * @brief Sorts the given vector using the bubble sort algorithm. + * + * This optimized bubble sort reduces the number of passes in average cases + * by checking if the array is already sorted during each pass. + * + * @param nums the vector of integers to be sorted + * @return reference to the sorted vector + */ +std::vector& bubble_sort(std::vector& nums) { + for (std::size_t i = 0; i < nums.size() - 1; i++) { + bool swapped = false; + for (std::size_t j = 0; j < nums.size() - 1 - i; j++) { + if (nums[j] > nums[j + 1]) { + std::swap(nums[j], nums[j + 1]); + swapped = true; + } } + if (!swapped) { + break; + } + } + return nums; +} - /** - * @brief Self-test implementations - * @returns void - */ - static void tests() { - std::vector v1 = {2,8,1,6,2,0,3,6}; - std::vector expected1 = {0,1,2,2,3,6,6,8}; - assert(bubble_sort(v1) == expected1); +/** + * @brief Self-test implementations of the bubble_sort function. + * + * Runs a set of predefined test cases using `assert` to ensure + * the correctness of the bubble_sort implementation. + * + * @returns void + */ +static void tests() { + std::vector v1 = {2, 8, 1, 6, 2, 0, 3, 6}; + const std::vector expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; + assert(bubble_sort(v1) == expected1); - std::vector v2 = {5,4,3,2,1}; - std::vector expected2 = {1,2,3,4,5}; - assert(bubble_sort(v2) == expected2); + std::vector v2 = {5, 4, 3, 2, 1}; + const std::vector expected2 = {1, 2, 3, 4, 5}; + assert(bubble_sort(v2) == expected2); - std::vector v3 = {1,2,3,4,5}; - std::vector expected3 = {1,2,3,4,5}; - assert(bubble_sort(v3) == expected3); + std::vector v3 = {1, 2, 3, 4, 5}; + const std::vector expected3 = {1, 2, 3, 4, 5}; + assert(bubble_sort(v3) == expected3); - std::cout << "All tests have successfully passed!\n"; - } + std::cout << "All tests have successfully passed!\n"; } +} // namespace sorting + /** * @brief Main function - * @returns 0 on exit + * + * Executes the self-test suite for bubble_sort. + * + * @returns 0 on successful execution */ int main() { - sorting::tests(); // run self-test implementations - return 0; + sorting::tests(); // run self-test implementations + return 0; } - From e89b5bf4b675d1e58f3ce5ae94a282ba10c64f4b Mon Sep 17 00:00:00 2001 From: Rohith A K Date: Thu, 25 Sep 2025 19:04:56 +0530 Subject: [PATCH 4/5] Tests are outside the namespace --- sorting/bubble_sort_optimized.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sorting/bubble_sort_optimized.cpp b/sorting/bubble_sort_optimized.cpp index 1a0fca50b4b..b9309b9d7a0 100644 --- a/sorting/bubble_sort_optimized.cpp +++ b/sorting/bubble_sort_optimized.cpp @@ -17,7 +17,7 @@ * // nums becomes {1, 2, 3, 4, 5} * @endcode * - * @author Rohith A K[https://github.com/rohleet] + * @author Rohith A K * @date 2025-09-25 */ @@ -53,6 +53,8 @@ std::vector& bubble_sort(std::vector& nums) { return nums; } +} // namespace sorting + /** * @brief Self-test implementations of the bubble_sort function. * @@ -64,21 +66,19 @@ std::vector& bubble_sort(std::vector& nums) { static void tests() { std::vector v1 = {2, 8, 1, 6, 2, 0, 3, 6}; const std::vector expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; - assert(bubble_sort(v1) == expected1); + assert(sorting::bubble_sort(v1) == expected1); std::vector v2 = {5, 4, 3, 2, 1}; const std::vector expected2 = {1, 2, 3, 4, 5}; - assert(bubble_sort(v2) == expected2); + assert(sorting::bubble_sort(v2) == expected2); std::vector v3 = {1, 2, 3, 4, 5}; const std::vector expected3 = {1, 2, 3, 4, 5}; - assert(bubble_sort(v3) == expected3); + assert(sorting::bubble_sort(v3) == expected3); std::cout << "All tests have successfully passed!\n"; } -} // namespace sorting - /** * @brief Main function * @@ -87,6 +87,6 @@ static void tests() { * @returns 0 on successful execution */ int main() { - sorting::tests(); // run self-test implementations + tests(); // run self-test implementations return 0; } From fd642d65924e5b6c4a418ebff5d85fcdeabcba29 Mon Sep 17 00:00:00 2001 From: Rohith A K Date: Tue, 30 Sep 2025 20:58:27 +0530 Subject: [PATCH 5/5] Now code is applicable for generic data types --- sorting/bubble_sort_optimized.cpp | 126 ++++++++++++++++++------------ 1 file changed, 77 insertions(+), 49 deletions(-) diff --git a/sorting/bubble_sort_optimized.cpp b/sorting/bubble_sort_optimized.cpp index b9309b9d7a0..e55c2528abf 100644 --- a/sorting/bubble_sort_optimized.cpp +++ b/sorting/bubble_sort_optimized.cpp @@ -1,11 +1,10 @@ /** * @file bubble_sort.cpp - * @brief Implementation of the Bubble Sort algorithm with self-tests. + * @brief Generic implementation of the Bubble Sort algorithm with self-tests. * - * This file contains an implementation of the bubble sort algorithm - * inside the `sorting` namespace. It includes an optimized version - * of bubble sort that reduces the number of passes when the input - * is already sorted or becomes sorted before completing all iterations. + * 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. @@ -15,78 +14,107 @@ * std::vector nums = {5, 4, 3, 2, 1}; * sorting::bubble_sort(nums); * // nums becomes {1, 2, 3, 4, 5} + * + * std::vector words = {"apple", "banana", "cherry"}; + * sorting::bubble_sort(words); + * // words becomes {"apple", "banana", "cherry"} * @endcode * * @author Rohith A K - * @date 2025-09-25 + * @date 2025-09-30 */ -#include /// for assert -#include /// for IO operations -#include /// for std::swap -#include /// for std::vector +#include ///< for assert +#include ///< for IO operations +#include ///< for std::swap +#include ///< for std::vector +#include ///< for std::string namespace sorting { /** - * @brief Sorts the given vector using the bubble sort algorithm. + * @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 array is already sorted during each pass. + * by checking if the container is already sorted during each pass. * - * @param nums the vector of integers to be sorted - * @return reference to the sorted vector + * @tparam Container Type of the container (e.g., std::vector) + * @param cont The container to be sorted + * @return Reference to the sorted container */ -std::vector& bubble_sort(std::vector& nums) { - for (std::size_t i = 0; i < nums.size() - 1; i++) { - bool swapped = false; - for (std::size_t j = 0; j < nums.size() - 1 - i; j++) { - if (nums[j] > nums[j + 1]) { - std::swap(nums[j], nums[j + 1]); - swapped = true; - } - } - if (!swapped) { - break; +template +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 nums; + return cont; } } // namespace sorting /** - * @brief Self-test implementations of the bubble_sort function. - * - * Runs a set of predefined test cases using `assert` to ensure - * the correctness of the bubble_sort implementation. - * - * @returns void + * @brief Self-test implementations of the generic bubble_sort function. */ static void tests() { - std::vector v1 = {2, 8, 1, 6, 2, 0, 3, 6}; - const std::vector expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; - assert(sorting::bubble_sort(v1) == expected1); + // Integer test cases + std::vector v1 = {2, 8, 1, 6, 2, 0, 3, 6}; + const std::vector expected1 = {0, 1, 2, 2, 3, 6, 6, 8}; + assert(sorting::bubble_sort(v1) == expected1); + + std::vector v2 = {5, 4, 3, 2, 1}; + const std::vector expected2 = {1, 2, 3, 4, 5}; + assert(sorting::bubble_sort(v2) == expected2); + + std::vector v3 = {1, 2, 3, 4, 5}; + const std::vector expected3 = {1, 2, 3, 4, 5}; + assert(sorting::bubble_sort(v3) == expected3); - std::vector v2 = {5, 4, 3, 2, 1}; - const std::vector expected2 = {1, 2, 3, 4, 5}; - assert(sorting::bubble_sort(v2) == expected2); + // Negative numbers + std::vector v4 = {-5, -1, -3, -2, -4}; + const std::vector expected4 = {-5, -4, -3, -2, -1}; + assert(sorting::bubble_sort(v4) == expected4); - std::vector v3 = {1, 2, 3, 4, 5}; - const std::vector expected3 = {1, 2, 3, 4, 5}; - assert(sorting::bubble_sort(v3) == expected3); + // Mixed negative and positive + std::vector v5 = {-1, 0, 1, -2, 2}; + const std::vector expected5 = {-2, -1, 0, 1, 2}; + assert(sorting::bubble_sort(v5) == expected5); - std::cout << "All tests have successfully passed!\n"; + // Empty vector + std::vector v6 = {}; + const std::vector expected6 = {}; + assert(sorting::bubble_sort(v6) == expected6); + + // Double test case + std::vector vd = {3.5, 2.1, 4.8, 1.2}; + const std::vector expectedd = {1.2, 2.1, 3.5, 4.8}; + assert(sorting::bubble_sort(vd) == expectedd); + + // String test case + std::vector vs = {"apple", "banana", "cherry", "apricot"}; + const std::vector expecteds = {"apple", "apricot", "banana", "cherry"}; + assert(sorting::bubble_sort(vs) == expecteds); + + // Char test case + std::vector vc = {'z', 'b', 'a', 'd', 'c'}; + const std::vector expectedc = {'a', 'b', 'c', 'd', 'z'}; + assert(sorting::bubble_sort(vc) == expectedc); + + std::cout << "All generic tests have successfully passed!\n"; } /** * @brief Main function - * - * Executes the self-test suite for bubble_sort. - * - * @returns 0 on successful execution */ int main() { - tests(); // run self-test implementations - return 0; + tests(); + return 0; }