对80,20,88,15,5这组数据用冒泡排序法排序
代码:
public class Demo {
public static void main(String[] args) {
int[] arr = {80,20,88,15,5};
for(int k = 0; k < arr.length - 1 ; k ++){//外层循环控制:循环次数;
for(int i = 0;i < arr.length - 1 - k ; i++){//从第一位,到相应位数的判断、交换;
if(arr[i] > arr[i + 1]){
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
printArray(arr);
}
public static void printArray(int[] arr){
StringBuffer buf = new StringBuffer();
buf.append("[");
for(int i = 0;i < arr.length ;i++){
buf.append(arr[i]);
if( i != arr.length - 1){
buf.append(",");
}
}
buf.append("]");
System.out.println(buf);
}
}
|