创建一个整数类型的数组,共10个元数,用Java程序对数组中的元数进行排序!!!!

重要的地方的代发,标注下,解释清楚些哦,谢谢了

public class Test {
public static void main(String[] args){
int[] a = {14,2,2,4,5,6,1,9,7,10};

//选择排序
/*int pos = 0;
for(int i = 0; i<a.length-1; i++){
for(int j = i; j<a.length-1; j++){
if(a[pos]>a[j+1]){
pos = j + 1;
}
}
//将最小值与队列首元素交换
int temp = a[i];
a[i] = a[pos];
a[pos] = temp;
//将pos的值恢复到正常位置
pos = i + 1;
}*/

//插入排序
for(int i = 1; i<a.length; i++){
int temp = a[i];//保存要插入的数

int m = i - 1;
while (m >=0 && a[m] > temp){//找到数据插入位置
a[m + 1] = a[m];
m--;
}

//将数字插入正确的位置
a[m + 1] = temp;
}

for(int i = 0; i<a.length; i++){
System.out.println(a[i]);
}
}
}

这里是选择排序和插入排序 冒泡排序就不需要说了吧?如果需要请HI我。
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-06-26
int[] i = new int[]{9,8,10,2,7,6,3,1,5,4};
i = Arrays.sort(i);
相似回答