美文网首页
阿健的刷题库函数|MyArrays

阿健的刷题库函数|MyArrays

作者: 阿健在长安 | 来源:发表于2018-08-14 18:06 被阅读10次
    package wsj;
    
    /**
     * 当前类所有函数 
     * ------------- 
     * 交换[范型]数组:swap() 
     * 打印一维[范型]数组:printArr1D()
     * 打印二维[范型]数组:printArr2D()
     * 冒泡排序一维[整型]数组,返回排序后的索引:BubbleSort1D() 
     * 冒泡排序一维[字符串型]数组,返回排序后的索引:BubbleSort1D()
     * 根据二维[整型]数组第一行进行冒泡排序:BubbleSort2D() 
     * 根据二维[字符串型]数组第一行进行冒泡排序:BubbleSort2D()
     */
    
    public class MyArrays {
        // 交换数组
        public static <T> void swap(T[] arr, int i, int j) {
            T tmp;
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    
        // 打印一维数据
        public static <T> void printArr1D(T[] arr) {
            String s = "[" + arr[0];
            for (int i = 1; i < arr.length; i++) {
                s += ", " + arr[i];
            }
            s += "]";
            System.out.println(s);
        }
    
        // 打印二维数据
        public static <T> void printArr2D(T[][] arr) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[0].length; j++) {
                    if (j < arr[0].length - 1) {
                        System.out.print(arr[i][j] + ",");
                    } else {
                        System.out.print(arr[i][j] + "\n");
                    }
                }
            }
            System.out.println();
        }
    
        // 冒泡排序一维整型数组,返回排序后的索引
        public static Integer[] BubbleSort1D(Integer[] arr) {
            if (arr == null || arr.length < 2) {
                return null;
            }
            if (arr.length < 2) {
                return new Integer[1];
            }
            Integer[] arrInd = new Integer[arr.length];
            for (int i = 0; i < arr.length; i++) {
                arrInd[i] = i;
            }
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        swap(arr, j, j + 1);
                        swap(arrInd, j, j + 1);
                    }
                }
            }
            return arrInd;
        }
    
        // 冒泡排序一维字符串型数组,返回排序后的索引
        public static Integer[] BubbleSort1D(String[] arr) {
            if (arr == null || arr.length < 2) {
                return null;
            }
            if (arr.length < 2) {
                return new Integer[1];
            }
            Integer[] arrInd = new Integer[arr.length];
            for (int i = 0; i < arr.length; i++) {
                arrInd[i] = i;
            }
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j].compareTo(arr[j + 1]) > 0) {
                        swap(arr, j, j + 1);
                        swap(arrInd, j, j + 1);
                    }
                }
            }
            return arrInd;
        }
    
        // 根据二维整型数组第一行进行冒泡排序
        public static void BubbleSort2D(Integer[][] arr) {
            Integer[] arrInd = BubbleSort1D(arr[0]);
            int[] help = new int[arr[0].length];
            for (int i = 0; i < help.length; i++) {
                help[i] = arr[1][arrInd[i]];
            }
            for (int i = 0; i < help.length; i++) {
                arr[1][i] = help[i];
            }
        }
    
        // 根据二维字符串型数组第一行进行冒泡排序
        public static void BubbleSort2D(String[][] arr) {
            Integer[] arrInd = BubbleSort1D(arr[0]);
            String[] help = new String[arr[0].length];
            for (int i = 0; i < help.length; i++) {
                help[i] = arr[1][arrInd[i]];
            }
            for (int i = 0; i < help.length; i++) {
                arr[1][i] = help[i];
            }
        }
    
        public static void main(String[] args) {
            Integer[][] arr = { { 3, 2, 1, 4 }, { 5, 4, 3, 6 } };
            BubbleSort2D(arr);
            printArr2D(arr);
    
            String[][] strings = { { "wsj", "hello", "abc", "abd" },
                    { "1", "2", "3", "4" } };
            BubbleSort2D(strings);
            printArr2D(strings);
        }
    }
    

    相关文章

      网友评论

          本文标题:阿健的刷题库函数|MyArrays

          本文链接:https://www.haomeiwen.com/subject/ngovbftx.html