diff --git a/algorithms/sorting/selectionSort.m b/algorithms/sorting/selectionSort.m new file mode 100644 index 0000000..cd067a1 --- /dev/null +++ b/algorithms/sorting/selectionSort.m @@ -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