Sorting algorithms/Cocktail sort: Difference between revisions

Added solution for MATLAB
m ({{eff note|J|/:~}})
(Added solution for MATLAB)
Line 614:
end
</lang>
 
 
=={{header|MATLAB}}==
 
<lang MATLAB>function list = cocktailSort(list)
%We have to do this because the do...while loop doesn't exist in MATLAB
swapped = true;
while swapped
%Bubble sort down the list
swapped = false;
for i = (1:numel(list)-1)
if( list(i) > list(i+1) )
temp = list(i);
list(i) = list(i+1);
list(i+1) = temp;
swapped = true;
end
end
if ~swapped
break
end
%Bubble sort up the list
swapped = false;
for i = (numel(list)-1:-1:1)
if( list(i) > list(i+1) )
temp = list(i);
list(i) = list(i+1);
list(i+1) = temp;
swapped = true;
end %if
end %for
end %while
end %cocktail sort</lang>
 
Sample Usage:
<lang MATLAB>cocktailSort([6 3 7 8 5 1 2 4 9])
 
ans =
 
1 2 3 4 5 6 7 8 9</lang>
 
=={{header|MAXScript}}==
Anonymous user