- Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Copy pathSolution.java
28 lines (24 loc) · 717 Bytes
/
Solution.java
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
importjava.util.Arrays;
publicclassSelectionSort {
privatestaticvoidselectionSort(int[] nums) {
for (inti = 0, n = nums.length; i < n - 1; ++i) {
intminIndex = i;
for (intj = i; j < n; ++j) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
swap(nums, minIndex, i);
}
}
privatestaticvoidswap(int[] nums, inti, intj) {
intt = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
publicstaticvoidmain(String[] args) {
int[] nums = {1, 2, 7, 9, 5, 8};
selectionSort(nums);
System.out.println(Arrays.toString(nums));
}
}