SIGN IN SIGN UP
doocs / leetcode UNCLAIMED

🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解

0 0 16 Java
2020-12-06 17:55:45 +08:00
import java.util.Arrays;
public class SelectionSort {
private static void selectionSort(int[] nums) {
for (int i = 0, n = nums.length; i < n - 1; ++i) {
int minIndex = i;
for (int j = i; j < n; ++j) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
swap(nums, minIndex, i);
}
}
private static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void main(String[] args) {
int[] nums = {1, 2, 7, 9, 5, 8};
selectionSort(nums);
System.out.println(Arrays.toString(nums));
}
}