From 055e24885e8889bf8f3a100834bb37ca8a209d14 Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Sat, 3 Oct 2020 12:02:57 +0530 Subject: [PATCH] added bubble, insertion and selection sort --- algorithms/sorting/bubblesort.m | 14 ++++++++++++++ algorithms/sorting/insertionsort.m | 14 ++++++++++++++ algorithms/sorting/selectionsort.m | 14 ++++++++++++++ readme.md | 8 ++++---- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 algorithms/sorting/bubblesort.m create mode 100644 algorithms/sorting/insertionsort.m create mode 100644 algorithms/sorting/selectionsort.m diff --git a/algorithms/sorting/bubblesort.m b/algorithms/sorting/bubblesort.m new file mode 100644 index 0000000..f5f583c --- /dev/null +++ b/algorithms/sorting/bubblesort.m @@ -0,0 +1,14 @@ +bubblesort(list) := + block( + [len: length(list)], + for i:1 thru len do ( + for j:1 thru (len - 1) do ( + if (list[j] > list[j + 1]) then ( + temp:list[j], + list[j]:list[j + 1], + list[j + 1]:temp + ) + ) + ), + list + ); diff --git a/algorithms/sorting/insertionsort.m b/algorithms/sorting/insertionsort.m new file mode 100644 index 0000000..f5b9b86 --- /dev/null +++ b/algorithms/sorting/insertionsort.m @@ -0,0 +1,14 @@ +insertionnsort(list) := + block( + [len: length(list)], + for i:2 thru len do ( + key:list[i], + j:(i - 1), + while j >= 1 and list[j] > key do ( + list[j + 1]:list[j], + j:(j - 1) + ), + list[j + 1]:key + ), + list + ); diff --git a/algorithms/sorting/selectionsort.m b/algorithms/sorting/selectionsort.m new file mode 100644 index 0000000..c4a540e --- /dev/null +++ b/algorithms/sorting/selectionsort.m @@ -0,0 +1,14 @@ +selectionnsort(list) := + block( + [len: length(list)], + for i:1 thru len do ( + min:i, + for j:(i + 1) thru len do ( + if (list[j] < list[min]) then min:j + ), + temp:list[min], + list[min]:list[i], + list[i]:temp + ), + list + ); diff --git a/readme.md b/readme.md index dd64172..c47b369 100644 --- a/readme.md +++ b/readme.md @@ -334,7 +334,7 @@ just a paper and pencil. - [Bead Sort](sorting/) - [Bogo Sort](sorting/) -- [Bubble Sort](sorting/) +- [Bubble Sort](algorithms/sorting/bubblesort.m) - [Bucket Sort](sorting/) - [Circle Sort](sorting/) - [Comb Sort](sorting/) @@ -343,14 +343,14 @@ just a paper and pencil. - [Flash Sort](sorting/) - [Gnome Sort](sorting/) - [Heap Sort](sorting/) -- [Insertion Sort](sorting/) +- [Insertion Sort](algorithms/sorting/insertionsort.m) - [Intro Sort](sorting/) - [Median Sort](sorting/) -- [Merge Sort](sorting/) +- [Merge Sort](algorithms/sorting/mergesort.m) - [Pipeonhole Sort](sorting/) - [Quick Sort](sorting/) - [Radix Sort](sorting/) -- [Selection Sort](sorting/) +- [Selection Sort](algorithms/sorting/selectionsort.m) - [Shaker Sort](sorting/) - [Shell Sort](sorting/) - [Sleep Sort](sorting/)