求解JAVA编程题:编写一个程序选择法对数组a[]={20,10,50,40,30,70,60,80,90,100}进行由大到小的顺序
1个回答
展开全部
public class C {
public static void main(String[] args) {
int[] ary = {20,10,50,40,30,70,60,80,90,100};
sort(ary);
System.out.println("After sort, array item is: ");
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
}
private static void sort(int[] ary) {
for(int i = 0; i < ary.length - 1; i++){
for(int j = i + 1; j < ary.length; j++){
if(ary[j] > ary[i]){
int temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
}
}
------------------testing
After sort, array item is:
100 90 80 70 60 50 40 30 20 10
public static void main(String[] args) {
int[] ary = {20,10,50,40,30,70,60,80,90,100};
sort(ary);
System.out.println("After sort, array item is: ");
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
}
private static void sort(int[] ary) {
for(int i = 0; i < ary.length - 1; i++){
for(int j = i + 1; j < ary.length; j++){
if(ary[j] > ary[i]){
int temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
}
}
------------------testing
After sort, array item is:
100 90 80 70 60 50 40 30 20 10
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询