-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
27 lines (24 loc) · 791 Bytes
/
SelectionSort.java
File metadata and controls
27 lines (24 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class SelectionSort {
public static int[] selectionSort(int[] arr) {
int minValue;
int minValueIndex;
int hValue;
// 1. iterate through whole array
for (int i = 0; i < arr.length; i++) {
minValue = arr[i];
minValueIndex = i;
// 2. from each arr starting point find the smallest value
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < minValue) {
minValue = arr[j];
minValueIndex = j;
}
}
// 3. change the smallest value with the currently arr value
hValue = arr[i];
arr[i] = minValue;
arr[minValueIndex] = hValue;
}
return arr;
}
}