/** * 选择排序 时间复杂度O(n^2) * @author Administrator * */ public class SelectSort { public static void main(String[] args) { int [] arr = {1,-3,4,-2,5,67,32,-4}; for (int i = 0; i < arr.length -1; i++) { int min = arr[i]; int minIndex=i; for (int j = i+1; j < arr.length; j++) { if (min > arr[j]) { min=arr[j]; minIndex=j; } } //将最小值放在arr[0]里面 if (i!=minIndex) { arr[minIndex]=arr[i]; arr[i]=min; } } System.out.println(Arrays.toString(arr)); } }