八大排序算法之 三 交换排序---冒泡排序算法

123

 

package primary;

public class BubbleSort {
    public static void main(String[] args){
         int[] array = {2,6,1,9,4,3,23,65,0,7};
         System.out.print("the array before is:");
         for(int i = 0; i < array.length; i++){
             System.out.print(array[i]+"  ");
         }
         System.out.println(" ");
         System.out.print("the array after is:");
         bubbleSort(array);
    }
    static void bubbleSort(int[] array){
        int len = array.length;
        for(int i = 0; i < len; i++){
            for(int j = 1; j < len - i; j++){
                if(array[j-1] > array[j]){
                    int temp = array[j-1];
                    array[j-1] =array[j];
                    array[j] = temp;
                }
            }
        }
        for(int z = 0; z < len; z++){
            System.out.print(array[z]+" ");
        }
    }
}

 

你可能感兴趣的:(冒泡排序)