Skip to content
Open
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
24 changes: 24 additions & 0 deletions algorithms/sorting/selectionSort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function list = selectionSort(list)

listSize = numel(list);

for i = (1:listSize-1)

minElem = list(i);
minIndex = i;

%This for loop can be vectorized, but there will be no significant
%increase in sorting efficiency.
for j = (i:listSize)
if list(j) <= minElem
minElem = list(j);
minIndex = j;
end
end

if i ~= minIndex
list([minIndex i]) = list([i minIndex]); %Swap
end

end %for
end %selectionSort