选择排序
选择排序:典型的比较排序
package org.study.order;
import java.util.Arrays;
public class SelectionSort01 {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 10, -1, 11};
int[] last = selectionSort(arr);
System.out.println(Arrays.toString(last));
}
public static int[] selectionSort(int[] arr) {
for (int end = arr.length - 1; end > 0; end --) {
int max = 0;
for (int begin = 1; begin < end; begin ++) {
if (compare(arr[max], arr[begin]) > 0) {
max = begin;
}
}
if (arr[max] > arr[end]) {
swap(max, end, arr);
}
}
return arr;
}
public static int compare(int a, int b) {
if (a >= b) {
return -1;
} else {
return 1;
}
}
public static void swap(int max, int min, int[] arr) {
int tmp = arr[max];
arr[max] = arr[min];
arr[min] = tmp;
}
}
程序改变世界