-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelection.java
More file actions
29 lines (25 loc) · 779 Bytes
/
Selection.java
File metadata and controls
29 lines (25 loc) · 779 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
28
29
public class Selection {
public static void sort(int[] input) {
// iterate over input
for (int i = 0; i < input.length; i++) {
// cache min index
int minIdx = i;
// look from i to end for smaller number
for (int j = i + 1; j < input.length; j++) {
if (input[j] < input[minIdx]) {
minIdx = j;
}
}
// put smallest number to the front
swap(input, i, minIdx);
}
}
// swaps index a and b
private static void swap(int[] input, int a, int b) {
if (a == b)
return;
int cache = input[a];
input[a] = input[b];
input[b] = cache;
}
}