简单的数组排序

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

}

posted @ 2018-11-30 11:44  山神笔记  阅读(197)  评论(0编辑  收藏  举报