From 5f354cb2045b6c296e1865b4b18d1e077dd5143d Mon Sep 17 00:00:00 2001 From: Kjetil Andre Johannessen Date: Fri, 29 Jun 2018 14:41:42 +0200 Subject: [PATCH 1/3] Added Matlab bubblesort --- book.json | 4 +++ .../sorting_searching/bubble/bubble_sort.md | 2 ++ .../bubble/code/matlab/bubblesort.m | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 chapters/sorting_searching/bubble/code/matlab/bubblesort.m diff --git a/book.json b/book.json index e2453bdf6..3b3df4a48 100644 --- a/book.json +++ b/book.json @@ -83,6 +83,10 @@ "lang": "go", "name": "Go" } + { + "lang": "m", + "name": "Matlab" + } ], "split": false diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 2ee5a945e..4e3d6d829 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -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" %} diff --git a/chapters/sorting_searching/bubble/code/matlab/bubblesort.m b/chapters/sorting_searching/bubble/code/matlab/bubblesort.m new file mode 100644 index 000000000..5e430622b --- /dev/null +++ b/chapters/sorting_searching/bubble/code/matlab/bubblesort.m @@ -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 + + From 6e0d40443fbb6aad39ee573a59e7a643b64bd6a0 Mon Sep 17 00:00:00 2001 From: Kjetil Andre Johannessen Date: Fri, 29 Jun 2018 14:54:54 +0200 Subject: [PATCH 2/3] Added matlab bogo sort --- chapters/sorting_searching/bogo/bogo_sort.md | 2 ++ .../bogo/code/matlab/bogosort.m | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 chapters/sorting_searching/bogo/code/matlab/bogosort.m diff --git a/chapters/sorting_searching/bogo/bogo_sort.md b/chapters/sorting_searching/bogo/bogo_sort.md index e8c91fec2..8a1db8d81 100644 --- a/chapters/sorting_searching/bogo/bogo_sort.md +++ b/chapters/sorting_searching/bogo/bogo_sort.md @@ -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" %} diff --git a/chapters/sorting_searching/bogo/code/matlab/bogosort.m b/chapters/sorting_searching/bogo/code/matlab/bogosort.m new file mode 100644 index 000000000..505d2e456 --- /dev/null +++ b/chapters/sorting_searching/bogo/code/matlab/bogosort.m @@ -0,0 +1,30 @@ +function main() + array = floor( rand(1,7)*100 ); + disp('Before Sorting:') + disp(array) + + array = bogoe_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 = bogoe_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 + + From ef9d0c15bb97eb8cff100466450006ad025d0c49 Mon Sep 17 00:00:00 2001 From: Kjetil Andre Johannessen Date: Fri, 29 Jun 2018 15:31:26 +0200 Subject: [PATCH 3/3] fixup! Added matlab bogo sort --- chapters/sorting_searching/bogo/code/matlab/bogosort.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapters/sorting_searching/bogo/code/matlab/bogosort.m b/chapters/sorting_searching/bogo/code/matlab/bogosort.m index 505d2e456..eeff29adb 100644 --- a/chapters/sorting_searching/bogo/code/matlab/bogosort.m +++ b/chapters/sorting_searching/bogo/code/matlab/bogosort.m @@ -3,7 +3,7 @@ function main() disp('Before Sorting:') disp(array) - array = bogoe_sort(array); + array = bogo_sort(array); disp('After Sorting') disp(array) end @@ -18,7 +18,7 @@ function main() retval = true; end -function sorted_array = bogoe_sort(array) +function sorted_array = bogo_sort(array) while ~is_sorted(array) % create a list of random permutation indices i = randperm(length(array));