冒泡排序算法(java实现)
Posted 光小林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了冒泡排序算法(java实现)相关的知识,希望对你有一定的参考价值。
public class HelloJava{
public static void main(String[] args) {
int[] arr1 = new int[] {20,40,90,30,80,70,50};
bubbleSort(arr1);
int[] arr2 = new int[] {11,52,4,5,65,4,2,1,45};
bubbleSort(arr2);
}
public static void bubbleSort(int[] array) {
System.out.println("排序前: ");
for(int i=0; i<array.length; i++) {
System.out.print(array[i]+" ");
}
System.out.print("
"+"冒泡排序后 : "+"
");
int flag;
for(int i=1; i<array.length; i++) {
flag = 0;
for(int j=0; j<array.length-i; j++) {
if(array[j] > array[j+1]) {
flag = 1;
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
System.out.print(array[j]+" ");
}
System.out.print("[ ");
for(int j=array.length-i; j<array.length; j++) {
System.out.print(array[j]+" ");
}
System.out.println("]");
if(flag == 0)
break;
}
}
}
以上是关于冒泡排序算法(java实现)的主要内容,如果未能解决你的问题,请参考以下文章