-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added Matlab bubblesort #166
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
Merged
jiegillet
merged 3 commits into
algorithm-archivists:master
from
VikingScientist:MatlabBubbleSort
Jun 29, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -83,6 +83,10 @@ | |
"lang": "go", | ||
"name": "Go" | ||
} | ||
{ | ||
"lang": "m", | ||
"name": "Matlab" | ||
} | ||
|
||
], | ||
"split": false | ||
|
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
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,30 @@ | ||
function main() | ||
array = floor( rand(1,7)*100 ); | ||
disp('Before Sorting:') | ||
disp(array) | ||
|
||
array = bogo_sort(array); | ||
disp('After Sorting') | ||
disp(array) | ||
end | ||
|
||
function retval = is_sorted(array) | ||
for i=1:length(array)-1 | ||
if array(i) > array(i+1) | ||
retval = false; | ||
return | ||
end | ||
end | ||
retval = true; | ||
end | ||
|
||
function sorted_array = bogo_sort(array) | ||
while ~is_sorted(array) | ||
% create a list of random permutation indices | ||
i = randperm(length(array)); | ||
array = array(i); | ||
end | ||
sorted_array = array; | ||
end | ||
|
||
|
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
25 changes: 25 additions & 0 deletions
25
chapters/sorting_searching/bubble/code/matlab/bubblesort.m
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,25 @@ | ||
function main() | ||
array = floor( rand(1,7)*100 ); | ||
disp('Before Sorting:') | ||
disp(array) | ||
|
||
array = bubble_sort(array); | ||
disp('After Sorting') | ||
disp(array) | ||
end | ||
|
||
function sorted_array = bubble_sort(array) | ||
for i=1:length(array) | ||
for j=1:length(array)-i | ||
if array(j) > array(j+1) | ||
% swap elements in the list | ||
temp = array(j); | ||
array(j) = array(j+1); | ||
array(j+1) = temp; | ||
end | ||
end | ||
end | ||
sorted_array = array | ||
end | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issorted()
is built-in, maybe use that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah know about this, but since we're teaching algorithms, it might be counterproductive to outsource too much to built-in methods. At the extreme case, you even have the
sort()
-function in matlab, but I don't think it would be advisable to use that one here. We just have to use individual discression in each case and for this particular case I opted to use the build-in functionrandperm
, but notissorted
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'm not going to insist on this one, your position is valid. It's just that, to me, the strength of matlab is the myriad of super useful built-in math functions that make your life easy. But you get it, you used
randperm
and I love that you implemented the shuffle that way!is_sorted
is a debatable, but usingsort()
would obviously defeat the purpose of implementing bubble sort :)Are you ready to merge or are you going to snipe-commit me again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready for merging. Promise I want do any more sniping ;)