java 选择排序法

时间:2024-01-10 19:30:32
 public class Test3 {

     /**@author shaobn
* @param 选择排序:将a.length - i个元素分别和第i个元素相比较,小的话就将值调换。依次递减进行排列
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a= {20,23,11,13,35,34,657};
Method1(a);
}
public static void Method1(int[] a){
int temp;
int value;
for (int i = 0; i < a.length; i++) {
temp = i;
for (int j = i+1; j < a.length; j++) {
if(a[j]<a[temp]){
temp = j;
}
}
value = a[i];
a[i] = a[temp];
a[temp] = value; }
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}