选择排序

//选择排序
//基本思想:从原始数组中寻找最小的那个数,然后与第一个数交换,依次类推
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {1, 4, 15, 63, 33, -1, 0, 2};
selectSort(arr);
System.out.println(Arrays.toString(arr));
}

public static void selectSort(int[] arr) {
int index,temp;
for (int i = 0; i < arr.length - 1; i++) {
index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;//交换元素位置下标
}
}
//元素交换
if (index != i) {
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
}
原文地址:https://www.cnblogs.com/jasonboren/p/10784002.html