码迷,mamicode.com
首页 > 编程语言 > 详细

简单的数组排序

时间:2018-11-30 14:11:30      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:except   print   interrupt   数组排序   简单   throw   inter   i++   exce   

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]);
}

}

简单的数组排序

标签:except   print   interrupt   数组排序   简单   throw   inter   i++   exce   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!