选择排序

//选择排序
//基本思想:从原始数组中寻找最小的那个数,然后与第一个数交换,依次类推
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;
}
}
}
}
posted @ 2019-04-28 14:56  jason小蜗牛  阅读(217)  评论(0编辑  收藏  举报