From d147cb52e2cb0548923eb45565f43e75a7e5ac8c Mon Sep 17 00:00:00 2001 From: Ozystar117 <77196588+Ozystar117@users.noreply.github.com> Date: Mon, 7 Jun 2021 20:40:01 +0100 Subject: [PATCH] Update SelectionSort.java --- .../activity/selectionsort/SelectionSort.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/packt/datastructuresandalg/lesson2/activity/selectionsort/SelectionSort.java b/src/main/java/com/packt/datastructuresandalg/lesson2/activity/selectionsort/SelectionSort.java index d466546..cc950a9 100644 --- a/src/main/java/com/packt/datastructuresandalg/lesson2/activity/selectionsort/SelectionSort.java +++ b/src/main/java/com/packt/datastructuresandalg/lesson2/activity/selectionsort/SelectionSort.java @@ -3,6 +3,25 @@ public class SelectionSort { public void sort(int[] array) { - + // the index of the minimum value + int minIndex = -1; + + // perform the operation n-1 times + for(int k = 0; k < arr.length; k++) { + // initial minimum value + int min = Integer.MAX_VALUE; + //loop through the values of the unsorted partition + for(int i = k; i < arr.length; i++) { + min = Math.min(min, arr[i]); + //update the index of the minimum value in the array + if(min == arr[i]) { + minIndex = i; + } + } + //swap + int temp = arr[k]; + arr[k] = arr[minIndex]; + arr[minIndex] = temp; + } } }