Skip to content

Commit e37385b

Browse files
authored
Create shell_sort.m
Sorting Algorithm
1 parent e7180a1 commit e37385b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

algorithms/sorting/shell_sort.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function list = shellSort(list)
2+
3+
N = numel(list);
4+
increment = round(N/2);
5+
6+
while increment > 0 %loop until increment becomes 0
7+
8+
for i = (increment+1:N)
9+
temp = list(i);
10+
j = i;
11+
while (j >= increment+1) && (list(j-increment) > temp)
12+
list(j) = list(j-increment);
13+
j = j - increment;
14+
end
15+
16+
list(j) = temp;
17+
18+
end %for
19+
20+
if increment == 2 %This case causes shell sort to become insertion sort
21+
increment = 1;
22+
else
23+
increment = round(increment/2.2);
24+
end
25+
end %while
26+
end %shellSort

0 commit comments

Comments
 (0)