一、思路
两两进行比较,大的在数组左边,小的在右边,这样每一次放在右边的一定是当次最小的
代码:
package com.s;
/*
*
*/
public class demo03 {
public static void main(String[] args) {
//
int[] arrays = {2,4,1,7,3,9,8};
//4,2,7,3,9,8,1
//4,7,3,9,8,2,1
//......
int temp=0;
for(int i =0;i<arrays.length-1;i++){
for(int j =0;j<arrays.length-1-i;j++){
//第一次循环6次
if(arrays[j]<arrays[j+1]){
temp = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1]= temp;
}
}
}
for (int array : arrays) {
System.out.print(array+" ");
}
}
}
结果