Java代码实现
public static void selectionSort(int[] arr) {
int len = arr.length;
for (int i = 0; i < len; i++) {
int minIndex = i;
int minValue = arr[i];
for (int j = i + 1; j < len; j++) {
if (arr[j] < minValue) {
minValue = arr[j];
minIndex = j;
}
}
arr[minIndex] = arr[i];
arr[i] = minValue;
}
}
Go代码实现
func selectionSort(arr []int) {
len := len(arr)
for i := 0; i < len; i++ {
minIndex := i
minValue := arr[i]
for j := i + 1; j < len; j++ {
if minValue > arr[j] {
minIndex = j
minValue = arr[j]
}
}
arr[minIndex] = arr[i]
arr[i] = minValue
}
}
网友评论