//直接选择排序算法
public class SelectSort {
public static void main (String[] args){
int[] array={63,4,24,1,3,65};
SelectSort sorter=new SelectSort();
sorter.sort(array);
}
public void sort(int[] array){
int index;
for(int i=1;i<array.length;i++){
index=0;
for (int j=1;j<=array.length-i;j++){
if (array[j]>array[index]){
index=j;
}
}
//交换在位置array.length-i和index(最大值)上的两个数
int temp=array[array.length-i]; //把第一个元素保存到临时变量中
array[array.length-i]=array[index]; //把第二个元素保存到第一个元素中
array[index]=temp; //把临时变量也就是第一个元素保存到第二个元素中
}
showArray(array); //输出排序后的结果
}
public void showArray(int[] array){
for (int i:array){ //遍历数组
System.out.println(">"+i);
}
System.out.println();
}
}
运行结果