java实现选择排序
实现代码
public class SelectSort {
public int[] selectSort(int[] array){
for(int i = 0;i < array.length - 1;i ++){
int minIndex = i;
for(int j = i + 1;j < array.length;j ++){
if(array[minIndex] > array[j])
minIndex = j;
}
if(minIndex != i){
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
return array;
}
}
public class Demo {
public static void main(String[] args) {
int[] array = {2,7,1,4,5,9,0,0,3,6,8};
SelectSort selectSort = new SelectSort();
array = selectSort.selectSort(array);
System.out.println("选择排序结果:");
for(int a : array)
System.out.print(a + " ");
}
}
效果
选择排序结果:
0 0 1 2 3 4 5 6 7 8 9