java冒泡排序

来源:互联网 发布:自贡广电网络 编辑:程序博客网 时间:2024/06/10 20:36
public static void main(String[] args){
int [] arr={9,3,8,2,6,7};
   printArray(arr);
bubblesort(arr);
printArray(arr);
}
public static void bubblesort(int[] arr){
int temp;
for(int i=0;i<arr.length-1;i++){
for(int j=arr.length-1;j>i;j--){

if(arr[j-1]>arr[j]){

temp = arr[j - 1];  
                   arr[j - 1] = arr[j];  
                   arr[j] = temp;       }
}
}
}
public static void printArray(int[] arr){
System.out.print("[");
for(int x=0;x<arr.length;x++){
if(x!=arr.length-1)
System.out.print(arr[x]+",");
else
System.out.println(arr[x]+"]");


}

}
1 0
原创粉丝点击