/** * 选择排序 * * 原理:将最小值与数组第1个即array[0]交换,第二次则忽略array[0],直接从array[1]至array[array.length-1]中 * 选择出最小值与array[1]交换,以此类推 * * 与冒泡排序法区别在于交换次数减少 * @param a * @return */ public long[] SelectSort(long[] a){ for(int i=0 ; i < a.length ; i++){ int minIndex = i; for(int j=i+1 ; j < a.length ; j++){ if(a[minIndex] > a[j]){ minIndex = j; } } if(i != minIndex){ a[i] = a[i] ^ a[minIndex]; a[minIndex] = a[minIndex] ^ a[i]; a[i] = a[i] ^ a[minIndex]; } } return a; }
原理:将最小值与数组第1个即array[0]交换,第二次则忽略array[0],直接从array[1]至array[array.length-1]中选择出最小值与array[1]交换,以此类推。