From 6da68c2c7e78f3d65f7a540229e5f7c526f44986 Mon Sep 17 00:00:00 2001 From: puneet-pr-arya <71585635+puneet-pr-arya@users.noreply.github.com> Date: Sun, 4 Oct 2020 11:56:13 +0530 Subject: [PATCH] Create selectionSort.m --- algorithms/sorting/selectionSort.m | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 algorithms/sorting/selectionSort.m 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