-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added an optimized bubble sort in sorts #3004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohleet
wants to merge
10
commits into
TheAlgorithms:master
Choose a base branch
from
rohleet:myContribution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
302b33e
Added an optimized bubble sort in sorts
rohleet 5d04574
Added necessary documentation and removed the using namespace
rohleet eaeba76
Merge branch 'master' into myContribution
rohleet 097d861
added file header documentation and clang format is followed
rohleet 6f0ed16
Merge branch 'myContribution' of https://github.com/rohleet/C-Plus-Pl…
rohleet e89b5bf
Tests are outside the namespace
rohleet 012dc82
Merge branch 'master' into myContribution
realstealthninja 7c044f9
Merge branch 'master' into myContribution
realstealthninja fd642d6
Now code is applicable for generic data types
rohleet 76cc35c
Merge branct 'myContribution' of https://github.com/rohleet/C-Plus-Pl…
rohleet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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<int> nums = {5, 4, 3, 2, 1}; | ||
* sorting::bubble_sort(nums); | ||
* // nums becomes {1, 2, 3, 4, 5} | ||
* @endcode | ||
* | ||
* @author Rohith A K | ||
* @date 2025-09-25 | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for IO operations | ||
#include <utility> /// for std::swap | ||
#include <vector> /// for std::vector | ||
|
||
namespace sorting { | ||
|
||
/** | ||
* @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<int>& bubble_sort(std::vector<int>& nums) { | ||
rohleet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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; | ||
} | ||
|
||
} // 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 | ||
*/ | ||
static void tests() { | ||
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); | ||
rohleet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
std::cout << "All 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.