Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions algorithms/sorting/cocktail_sort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function list = cocktailSort(list)

%since the do-while loop doesn't exist in MATLAB we will perform following steps
swapped = true;

while swapped

%Bubble sort down the list
swapped = false;
for i = (1:numel(list)-1)
if( list(i) > list(i+1) )
list([i i+1]) = list([i+1 i]); %swap
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) )
list([i i+1]) = list([i+1 i]); %swap
swapped = true;
end %if
end %for
end %while
end %cocktail sort