Skip to content

Commit 32e36d0

Browse files
authored
Create cocktail_sort.m
Sorting Algorithm
1 parent e7180a1 commit 32e36d0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

algorithms/sorting/cocktail_sort.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function list = cocktailSort(list)
2+
3+
%since the do-while loop doesn't exist in MATLAB we will perform following steps
4+
swapped = true;
5+
6+
while swapped
7+
8+
%Bubble sort down the list
9+
swapped = false;
10+
for i = (1:numel(list)-1)
11+
if( list(i) > list(i+1) )
12+
list([i i+1]) = list([i+1 i]); %swap
13+
swapped = true;
14+
end
15+
end
16+
17+
if ~swapped
18+
break
19+
end
20+
21+
%Bubble sort up the list
22+
swapped = false;
23+
for i = (numel(list)-1:-1:1)
24+
if( list(i) > list(i+1) )
25+
list([i i+1]) = list([i+1 i]); %swap
26+
swapped = true;
27+
end %if
28+
end %for
29+
end %while
30+
end %cocktail sort

0 commit comments

Comments
 (0)