We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8fcfdf9 + 32e36d0 commit 98b205aCopy full SHA for 98b205a
algorithms/sorting/cocktail_sort.m
@@ -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
14
+ end
15
16
17
+ if ~swapped
18
+ break
19
20
21
+ %Bubble sort up the list
22
23
+ for i = (numel(list)-1:-1:1)
24
25
26
27
+ end %if
28
+ end %for
29
+ end %while
30
+end %cocktail sort
0 commit comments