排序!!!

 

class ArrayDemo{
    /*
        选择排序
    */
    public static void selectSort(int[] arr){
        for(int x=0; x<arr.length-1 ; x++){
            for(int y=x+1; y<arr.length; y++){
                if(arr[x] > arr[y]){
                    int temp = arr[x];
                    arr[x] = arr[y];
                    arr[y] = temp;
                }
            }
        }
    }

    /*
        冒泡排序
    */
    public static void mpSort(int[] arr){
        for(int x=0 ; x<arr.length-1 ; x++){
            for(int y=0 ; y<arr.length-1 ; y++){
                if(arr[y]>arr[y+1]){
                    int temp = arr[y];
                    arr[y] = arr[y+1];
                    arr[y+1] = temp;
                }
            }
        }
    }

    public static void main(String[] args){
        int[] arr = {23,51,12,54,23,4,1,43};
        
        // 选择排序
        // selectSort(arr);

        // 冒泡排序
        mpSort(arr);

        for(int x=0 ; x<arr.length; x++){
            System.out.print(arr[x]+",");
        }

    }

}

 

posted @ 2016-08-29 15:45  hoey94  阅读(111)  评论(0编辑  收藏  举报