From e37385b284911f1aea7bedd70b2d1b5d50391ce0 Mon Sep 17 00:00:00 2001 From: Abhishek Mehra <52788025+Triaro@users.noreply.github.com> Date: Fri, 2 Oct 2020 10:47:48 +0530 Subject: [PATCH] Create shell_sort.m Sorting Algorithm --- algorithms/sorting/shell_sort.m | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithms/sorting/shell_sort.m diff --git a/algorithms/sorting/shell_sort.m b/algorithms/sorting/shell_sort.m new file mode 100644 index 0000000..c588cb1 --- /dev/null +++ b/algorithms/sorting/shell_sort.m @@ -0,0 +1,26 @@ +function list = shellSort(list) + + N = numel(list); + increment = round(N/2); + + while increment > 0 %loop until increment becomes 0 + + for i = (increment+1:N) + temp = list(i); + j = i; + while (j >= increment+1) && (list(j-increment) > temp) + list(j) = list(j-increment); + j = j - increment; + end + + list(j) = temp; + + end %for + + if increment == 2 %This case causes shell sort to become insertion sort + increment = 1; + else + increment = round(increment/2.2); + end + end %while +end %shellSort