要点:从一组数据,找出最大或者最小的数据,与开始位置的数据进行交换。
public class SelectSort { public static void main(String[] args) { //int[] arr={101,34,119,1}; //int[] arr={9,5,6,99,45,2}; int maxLength=80000; int[] arr=new int[maxLength]; for (int i = 0; i < maxLength; i++) { arr[i]= (int) (Math.random()*maxLength); } //System.out.println(Arrays.toString(arr)); long start = System.currentTimeMillis(); selectSort(arr); long end = System.currentTimeMillis(); System.out.println(end-start); //System.out.println(Arrays.toString(arr)); } private static void selectSort(int[] arr) { int min=arr[0]; int minIndex=0; for (int i = 0; i < arr.length-1; i++) { min=arr[i]; minIndex=i; for (int j = i+1; j < arr.length; j++) { if (arr[j]<min) { min=arr[j]; minIndex=j; } } if (minIndex!=i) { arr[minIndex]=arr[i]; arr[i]=min; } } } }