Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"lang": "go",
"name": "Go"
}
{
"lang": "m",
"name": "Matlab"
}

],
"split": false
Expand Down
2 changes: 2 additions & 0 deletions chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ In code, it looks something like this:
[import:1-16, lang:"javascript"](code/js/bogo.js)
{% sample lang="hs" %}
[import, lang:"haskell"](code/haskell/bogoSort.hs)
{% sample lang="m" %}
[import, lang:"matlab"](code/matlab/bogosort.m)
{% sample lang="cpp" %}
[import, lang:"c_cpp"](code/c++/bogosort.cpp)
{% sample lang="rs" %}
Expand Down
30 changes: 30 additions & 0 deletions chapters/sorting_searching/bogo/code/matlab/bogosort.m
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)
Copy link
Member

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.

Copy link
Contributor Author

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 function randperm, but not issorted.

Copy link
Member

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 using sort() would obviously defeat the purpose of implementing bubble sort :)

Are you ready to merge or are you going to snipe-commit me again?

Copy link
Contributor Author

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 ;)

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


2 changes: 2 additions & 0 deletions chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-11, lang:"javascript"](code/js/bubble.js)
{% sample lang="py" %}
[import:4-9, lang:"python"](code/python/bubblesort.py)
{% sample lang="m" %}
[import:11-23, lang:"matlab"](code/matlab/bubblesort.m)
{% sample lang="hs" %}
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
{% sample lang="cpp" %}
Expand Down
25 changes: 25 additions & 0 deletions chapters/sorting_searching/bubble/code/matlab/bubblesort.m
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