选择排序的概念:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。public class SelectSort {public final static void swap(int a[], int i, int j) {int temp = a[i];a[i] = a[j];a[j] = temp;}public final static void selectSort(int a[],int low,int high){ for(int i = high;--i>low;){ int max = i; for(int j = i;--j>=low;){ if(a[j]>a[max])max=j; } if(max!=i)swap(a,max,i); System.out.println(Arrays.toString(a)); } } public final static void selectSort(int a[]){ selectSort(a,0,a.length); } public static void main(String[] args) {int a[] = new int[] { 5, 4, 3, 2, 1 }; System.out.println(Arrays.toString(a));selectSort(a);}}
运行结果是:
[5, 4, 3, 2, 1][1, 4, 3, 2, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5]