-
-
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 1 commit
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,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 <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
vector<int>& bubble_sort(vector<int>& nums){ | ||
int flag=0; | ||
int temp; | ||
for(int i=0; i<nums.size()-1;i++){ | ||
flag=0; | ||
for(int j=0; j<nums.size()-1-i;j++){ | ||
if(nums[j]>nums[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<int> v={2,8,1,6,2,0,3,6}; | ||
v=bubble_sort(v); | ||
for(int i=0;i<v.size();i++){ | ||
cout<<v.at(i)<<endl; | ||
} | ||
return 0; | ||
} | ||
rohleet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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.