Skip to content

Commit b5af512

Browse files
VikingScientistjiegillet
authored andcommitted
Added Matlab bubblesort (#166)
* Added Matlab bubblesort * Added matlab bogo sort
1 parent 6358f9a commit b5af512

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
"lang": "go",
8484
"name": "Go"
8585
}
86+
{
87+
"lang": "m",
88+
"name": "Matlab"
89+
}
8690

8791
],
8892
"split": false

chapters/sorting_searching/bogo/bogo_sort.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ In code, it looks something like this:
2727
[import:1-16, lang:"javascript"](code/js/bogo.js)
2828
{% sample lang="hs" %}
2929
[import, lang:"haskell"](code/haskell/bogoSort.hs)
30+
{% sample lang="m" %}
31+
[import, lang:"matlab"](code/matlab/bogosort.m)
3032
{% sample lang="cpp" %}
3133
[import, lang:"c_cpp"](code/c++/bogosort.cpp)
3234
{% sample lang="rs" %}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function main()
2+
array = floor( rand(1,7)*100 );
3+
disp('Before Sorting:')
4+
disp(array)
5+
6+
array = bogo_sort(array);
7+
disp('After Sorting')
8+
disp(array)
9+
end
10+
11+
function retval = is_sorted(array)
12+
for i=1:length(array)-1
13+
if array(i) > array(i+1)
14+
retval = false;
15+
return
16+
end
17+
end
18+
retval = true;
19+
end
20+
21+
function sorted_array = bogo_sort(array)
22+
while ~is_sorted(array)
23+
% create a list of random permutation indices
24+
i = randperm(length(array));
25+
array = array(i);
26+
end
27+
sorted_array = array;
28+
end
29+
30+

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2020
[import:1-11, lang:"javascript"](code/js/bubble.js)
2121
{% sample lang="py" %}
2222
[import:4-9, lang:"python"](code/python/bubblesort.py)
23+
{% sample lang="m" %}
24+
[import:11-23, lang:"matlab"](code/matlab/bubblesort.m)
2325
{% sample lang="hs" %}
2426
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2527
{% sample lang="cpp" %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function main()
2+
array = floor( rand(1,7)*100 );
3+
disp('Before Sorting:')
4+
disp(array)
5+
6+
array = bubble_sort(array);
7+
disp('After Sorting')
8+
disp(array)
9+
end
10+
11+
function sorted_array = bubble_sort(array)
12+
for i=1:length(array)
13+
for j=1:length(array)-i
14+
if array(j) > array(j+1)
15+
% swap elements in the list
16+
temp = array(j);
17+
array(j) = array(j+1);
18+
array(j+1) = temp;
19+
end
20+
end
21+
end
22+
sorted_array = array
23+
end
24+
25+

0 commit comments

Comments
 (0)