冒泡算法 2015-07-26 11:45 2人阅读 评论(0) 收藏

package sort;

public class BubbleSort {
    public static int arr[]=new int[10];
    static{
        System.out.println("before sort:");
        for(int i=0;i<arr.length;i++){
            arr[i]=(int)(Math.random()*100);
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    } 
    public static void main(String args[]){
        int m=arr.length-1;
        bubbleSort(m);
        System.out.println("after sort:");
        for(int i=0;i<=m;i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    public static void bubbleSort(int m){
        int temp=0;
        for(int i=m;i>0;i--){
            for(int j=0;j<i;j++){
                if(arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
            System.out.print("current sorting"+i+": ");
            for(int k=0;k<=m;k++)
                System.out.print(arr[k]+" ");
            System.out.println();
        }
    }
}
  

 

posted @ 2015-07-26 11:45  吕布布1995  阅读(91)  评论(0编辑  收藏  举报