zoukankan      html  css  js  c++  java
  • 中国大学MOOC-数据结构基础习题集、07-2、Insert or Merge

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main {
        static String line;
        static int N;
        static int[] test1;
        static int[] test2;
        static int[] check;
    
        public static void main(String[] args) throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            line = in.readLine();
            N = Integer.parseInt(line);
            line = in.readLine();
            String[] string = line.split(" ");
            test1 = new int[N];
            test2 = new int[N];
            check = new int[N];
            for (int i = 0; i < N; i++) {
                test1[i] = Integer.parseInt(string[i]);
            }
            for (int i = 0; i < N; i++) {
                test2[i] = Integer.parseInt(string[i]);
            }
            line = in.readLine();
            string = line.split(" ");
            for (int i = 0; i < N; i++) {
                check[i] = Integer.parseInt(string[i]);
            }
            Merge_sort(test1, N);
            
            Insert_sort(test2, N);
            
    
        }
        
        public static void ouput(int[] n){
            for (int i = 0; i < N - 1; i++) {
                System.out.print(n[i]);
                System.out.print(" ");
            }
            System.out.print(n[N - 1]);
        }
        static boolean isEqual(int a[], int b[], int n)
        {
            for(int i=0; i<n; i++)
                if(a[i] != b[i])
                    return false;
            return true;
        }
    
        static void Insert_sort(int[] A, int N) {
            boolean flag=false;
            for (int i = 1; i < N; i++) {
                int temp = A[i];
                int j;
                for (j = i; j > 0 && A[j - 1] > temp; j--) {
                    A[j] = A[j - 1];
                }
                A[j] = temp;
                if(flag==true){
                    System.out.println("Insertion Sort");
                    ouput(A);
                    break;
                }
                if(isEqual(A,check,N)){
                    flag=true;
                }
                if(flag==true&&i==(N-1)){
                    System.out.println("Insertion Sort");
                    ouput(A);
                    break;
                }
            }
        }
    
        static void Merge_sort(int[] A, int N) {
            boolean flag=false;
            int length = 1;
            int[] TmpA = new int[N];
            while (length < N) {
                Merge_pass(A, TmpA, N, length);
                if(flag==true){
                    System.out.println("Merge Sort");
                    ouput(A);
                    break;
                }
                if(isEqual(A,check,N)){
                    flag=true;
                }
                length *= 2;
                Merge_pass(TmpA, A, N, length);
                if(flag==true){
                    System.out.println("Merge Sort");
                    ouput(A);
                    break;
                }
                if(isEqual(A,check,N)){
                    flag=true;
                }
                length *= 2;
                if(flag==true&&length>N){
                    System.out.println("Merge Sort");
                    ouput(A);
                    break;
                }
            }
        }
    
        static void Merge_pass(int[] A, int[] TmpA, int N, int length) {// length当前有序子列的长度
            int i;
            for (i = 0; i <= N - 2 * length; i += 2 * length) {
                Merge(A, TmpA, i, i + length, i + 2 * length - 1);
            }
            if (i + length < N)
                Merge(A, TmpA, i, i + length, N - 1);
            else
                for (int j = i; j < N; j++)
                    TmpA[j] = A[j];
    
        }
    
        /**
         * 
         * @param A
         * @param L
         *            左边起始位置
         * @param R
         *            右边起始位置
         * @param RightEnd
         *            右边终点位置
         * @return
         */
        static void Merge(int[] A, int[] TmpA, int L, int R, int RightEnd) {
            int LeftEnd = R - 1;// 左边终点位置。假设左右两列挨着
            int Tmp = L;// 存放结果的数组的初始位置
            int NumElements = RightEnd - L + 1;// 元素总数
            while (L <= LeftEnd && R <= RightEnd) {
                if (A[L] < A[R])
                    TmpA[Tmp++] = A[L++];
                else
                    TmpA[Tmp++] = A[R++];
            }
            while (L <= LeftEnd) {// 直接复制左边剩下的
                TmpA[Tmp++] = A[L++];
            }
            while (R <= RightEnd) {// 直接复制右边剩下的
                TmpA[Tmp++] = A[R++];
            }
            for (int i = 0; i < NumElements; i++, RightEnd--) {
                A[RightEnd] = TmpA[RightEnd];
            }
        }
    }

  • 相关阅读:
    Gym 100553B Burrito King 无脑背包
    BestCoder Round #85 A B C
    poj 1687 Buggy Sat 简单计算几何
    HDU 1863 Kruskal求最小生成树
    记2016商大ACM省赛
    COMP9517 Week7 Tracking
    COMP9517 week7 Motion
    COMP9313 week7b Spark SQL
    COMP9313 Week 7 Product Quantization and K-Means Clustering
    COMP9517 lab3 image segementation
  • 原文地址:https://www.cnblogs.com/keitho00/p/4250498.html
Copyright © 2011-2022 走看看