首页 > 编程语言 > 详细

简单的数组排序

时间:2018-11-30 12:38:00      阅读:122      评论:0      收藏:0      [点我收藏+]

1.小到大

@org.junit.Test
public void test() throws InterruptedException {
int[] num={1,6,5,2,7};
for (int i=0;i<num.length-1;i++) {
for (int j = 0; j <num.length-1; j++) {
int b = 0;
if (num[j]>num[j+1]){
b = num[j];
num[j] = num[j + 1];
num[j+ 1] = b;
}

}
}

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

}
2.大到小
@org.junit.Test
public void test() throws InterruptedException {
int[] num={1,6,5,2,7};
for (int i=0;i<num.length-1;i++) {
for (int j = 0; j <num.length-1; j++) {
int b = 0;
if (num[j]<num[j+1]){ //这里不一样
b = num[j];
num[j] = num[j + 1];
num[j+ 1] = b;
}

}
}

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

}

简单的数组排序

原文:https://www.cnblogs.com/shanshen/p/10043289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!