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