首页 > 代码库 > JAVA中,数组的操作与排序

JAVA中,数组的操作与排序

自己写了正向反向的冒泡排序,还用了静态和NEW方法实现。

import java.util.Arrays;


public class HelloJava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[][] = new int[3][4];
        for (int i = 0; i < a.length; i++){
            for (int j = 0; j < a[i].length; j++){
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
        int baar[] = new int[]{3,5,12,345,543,168,98,670,5436,12454,87678,987,435,675,234,67};
        //Arrays.sort(baar);
        //int index = Arrays.binarySearch(baar, 12);
        //System.out.println(index);
        sortA(baar);
        ShowArray(baar);
        System.out.println();
        HelloJava sorter = new HelloJava();
        sorter.sortB(baar);
        ShowArray(baar);


    }
    static void sortA(int[] arr){
        for(int i = 1; i < arr.length; i++){
            for(int j = 0; j < i; j++){
                if (arr[i] > arr[j]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                            
                }
            }
        }
    }
    private void sortB(int[] arr){
        for(int i = 1; i < arr.length; i++){
            for(int j = 0; j < i; j++){
                if (arr[i] < arr[j]){
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                            
                }
            }
        }
    }
    static void ShowArray(int[] aar){
        for (int i = 0; i < aar.length; i++){    
            System.out.print(aar[i]);
        System.out.print(",");
    }
    }

}