public class Java_16{
/** 选择排序
定义一个数组长度为5的数组,下标为0的数依次与
下标为1、2、3、4的数比较,下标为1的数
依次与 下标为2、3、4的数比较,下标为2的数
依次与 下标为3、4的数比较,下标为3的数
与 下标为4的数比较,然后按从小到大的顺序排列
public static void main(String[] args) {
int temp[]={8,10,6,5,9}; //定义一个长度为5的数组
fun(temp); //调用fun方法
print (temp); //调用print方法
}
public static void fun(int temp[]){
for(int i=0; i<temp.length-1; i++){ //外循环
for(int j=i+1; j<temp.length; j++){ //内循环
if(temp[i]>temp[j]){ //比较大小
int x=temp[i]; //把数组i的值赋值给变量 x
temp[i]=temp[j]; //交换位置
temp[j]=x; //把变量x的值赋值给数组j
}
}
}
}
public static void print(int temp[]){
for(int i=0; i<temp.length; i++){ //循环遍历
System.out.print(temp[i]+"\t"); //打印数组i的值
}
}
}