您的位置:首页 > 编程语言 > Java开发

java冒泡排序

2016-05-01 23:14 281 查看
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]+"]");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: