Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将两个数组合并到一个按升序排列的数组中,该数组无法正常工作。JAVA_Java_Arrays_Sorting_Merge - Fatal编程技术网

将两个数组合并到一个按升序排列的数组中,该数组无法正常工作。JAVA

将两个数组合并到一个按升序排列的数组中,该数组无法正常工作。JAVA,java,arrays,sorting,merge,Java,Arrays,Sorting,Merge,我正在尝试将两个数组排序为一个数组。 但是我有一些问题。排序不正确。我已经附上了文件,代码和输出 import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class MArray { public static void mergeA(long[] A, long[] B) { long [] merged =

我正在尝试将两个数组排序为一个数组。 但是我有一些问题。排序不正确。我已经附上了文件,代码和输出

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class MArray {


public static void mergeA(long[] A, long[] B) {

long [] merged = new long[A.length + B.length ];
int indexFirst = 0, indexSecond = 0, indexMerge = 0;

while (indexFirst < A.length && indexSecond < B.length) {
    if (A[indexFirst] <= B[indexSecond]) {
        merged[indexMerge++] = A[indexFirst++];
    }
    else {           
        merged[indexMerge++] = B[indexSecond++];
    }
}


    System.out.print("\n");
    System.out.println("Here is your merged array: " );

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


}

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub

    long array1[] = null;
    long array2[] = null;


    Scanner Scanscan = new Scanner(System.in);
    System.out.print("Input filename: ");
    String filename = Scanscan.nextLine();
    File inputFile = new File(filename);
    Scanner reader = new Scanner(inputFile);

    int i = 0;
    long array[] = new long[20];
    while(reader.hasNext())
    {
        array[i] = reader.nextInt();
        i++;

    }
    array1 = new long[i];
    System.arraycopy(array, 0, array1, 0, i);
    Arrays.sort(array1);

    for (int i1 = 0; i1 < array1.length; i1++) {
           System.out.print(array1[i1] + " ");
        }



    System.out.println( "\n");


    System.out.println("Please enter your second file name: ");

    String filename2 = Scanscan.nextLine();
    File inputFile2 = new File(filename2);
    Scanner reader2 = new Scanner(inputFile2);

      int i1 = 0;
        long temp1[] = new long[20];
        while(reader2.hasNext())
        {
            temp1[i1] = reader2.nextInt();
            i1++;

        }
        array2 = new long[i1];
        System.arraycopy(temp1, 0, array2, 0, i1);
        Arrays.sort(array2);

        for (int i11 = 0; i11 < array2.length; i11++) {
               System.out.print(array2[i11] + " ");
            }



    mergeA(array1, array2);


}

}

如果您确实需要将未排序的数组作为潜在输入,那么可以使用Merge-Sort对两个单独的数组进行排序,然后执行最后一个合并操作来连接两个排序的数组

以下是使用初始未排序输入的测试运行:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class MArray {

  public static void mergeA(long[] A, long[] B) {

    long [] merged = new long[A.length + B.length ];
    int indexFirst = 0, indexSecond = 0, indexMerge = 0;

    while (indexFirst < A.length && indexSecond < B.length) {
        if (A[indexFirst] <= B[indexSecond]) {
            merged[indexMerge++] = A[indexFirst++];
        }
        else {           
            merged[indexMerge++] = B[indexSecond++];
        }
    }

    //get remaining items if arrays were not equal lengths

    while (indexFirst < A.length){
       merged[indexMerge++] = A[indexFirst++];
    }

    while (indexSecond < B.length){
       merged[indexMerge++] = B[indexSecond++];
    }


    System.out.print("\n");
    System.out.println("Here is your merged array: " );

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


  }

  public static void main(String[] args) throws FileNotFoundException {
      // TODO Auto-generated method stub

      long array1[] = null;
      long array2[] = null;

  /*
      Scanner Scanscan = new Scanner(System.in);
      System.out.print("Input filename: ");
      String filename = Scanscan.nextLine();
      File inputFile = new File(filename);
      Scanner reader = new Scanner(inputFile);

      int i = 0;
      while(reader.hasNextInt())
      {
           array1[i++] = reader.nextInt();
      }

      for (int i1 = 0; i1 < array1.length; i1++) {
             System.out.print(array1[i1] + " ");
          }
      System.out.println( "\n");


      System.out.println("Please enter your second file name: ");

      String filename2 = Scanscan.nextLine();
      File inputFile2 = new File(filename2);
      Scanner reader2 = new Scanner(inputFile2);

      int i1 = 0;

      while(reader2.hasNextInt()){
         array2[i1++] = reader2.nextInt();
      }
       System.out.println("  ");

       for (int i11 = 0; i11 < array2.leng= th; i11++) {
             System.out.print(array2[i11] + " ");
          }
          */


    long[] a1 = new long[]{1, 9, 6, 11, 12, 4, 7, 2};
    long[] a2 = new long[]{2, 8, 3, 13, 5, 10};

    mergeSort(a1);
    mergeSort(a2);

    array1 = new long[a1.length];
    System.arraycopy(a1, 0, array1, 0, a1.length);

    array2 = new long[a2.length];
    System.arraycopy(a2, 0, array2, 0, a2.length);

    mergeA(array1, array2);

  }

  static void mergeSort(long[] array){
    long[] helper = new long[array.length];
    mergeSort(array, helper, 0, array.length - 1);
  }

  static void mergeSort(long[] array, long[] helper, int low, int high){
    if (low < high){
      int middle = (low + high) / 2;
      mergeSort(array, helper, low, middle);
      mergeSort(array, helper, middle + 1, high);
      merge(array, helper, low, middle, high);
    }
  }

  static void merge(long[] array, long[] helper, int low, int middle, int high){
    for (int i = low; i <= high; i++){
      helper[i] = array[i];
    }

    int helperLeft = low;
    int helperRight = middle + 1;
    int current = low;
    while (helperLeft <= middle && helperRight <= high){
      if (helper[helperLeft] <= helper[helperRight]){
        array[current++] = helper[helperLeft++];
      }
      else{
        array[current++] = helper[helperRight++];
      }
    }
    while (helperLeft <= middle){
      array[current++] = helper[helperLeft++];
    }
  }


}

当您将数组声明为int[20]时,您将得到一个填充了20个零的数组。合并排序的mergeint[],int[]方法不是合并两个已排序的数组吗?我想你需要仔细阅读合并排序。找一个调试器但是我的教授说把所有数组的大小设置为20…有没有办法解决这个问题?合并排序不会合并两个数组。它将一个原始数组拆分,然后按照我知道的正确顺序将其合并在一起进行排序。基本上,当使用从一个数组开始的合并排序时,不会从两个数组开始。我的教授说要将两个数组合并成一个升序数组。然后让第三个按顺序抓住它们。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class MArray {

  public static void mergeA(long[] A, long[] B) {

    long [] merged = new long[A.length + B.length ];
    int indexFirst = 0, indexSecond = 0, indexMerge = 0;

    while (indexFirst < A.length && indexSecond < B.length) {
        if (A[indexFirst] <= B[indexSecond]) {
            merged[indexMerge++] = A[indexFirst++];
        }
        else {           
            merged[indexMerge++] = B[indexSecond++];
        }
    }

    //get remaining items if arrays were not equal lengths

    while (indexFirst < A.length){
       merged[indexMerge++] = A[indexFirst++];
    }

    while (indexSecond < B.length){
       merged[indexMerge++] = B[indexSecond++];
    }


    System.out.print("\n");
    System.out.println("Here is your merged array: " );

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


  }

  public static void main(String[] args) throws FileNotFoundException {
      // TODO Auto-generated method stub

      long array1[] = null;
      long array2[] = null;

  /*
      Scanner Scanscan = new Scanner(System.in);
      System.out.print("Input filename: ");
      String filename = Scanscan.nextLine();
      File inputFile = new File(filename);
      Scanner reader = new Scanner(inputFile);

      int i = 0;
      while(reader.hasNextInt())
      {
           array1[i++] = reader.nextInt();
      }

      for (int i1 = 0; i1 < array1.length; i1++) {
             System.out.print(array1[i1] + " ");
          }
      System.out.println( "\n");


      System.out.println("Please enter your second file name: ");

      String filename2 = Scanscan.nextLine();
      File inputFile2 = new File(filename2);
      Scanner reader2 = new Scanner(inputFile2);

      int i1 = 0;

      while(reader2.hasNextInt()){
         array2[i1++] = reader2.nextInt();
      }
       System.out.println("  ");

       for (int i11 = 0; i11 < array2.leng= th; i11++) {
             System.out.print(array2[i11] + " ");
          }
          */


    long[] a1 = new long[]{1, 9, 6, 11, 12, 4, 7, 2};
    long[] a2 = new long[]{2, 8, 3, 13, 5, 10};

    mergeSort(a1);
    mergeSort(a2);

    array1 = new long[a1.length];
    System.arraycopy(a1, 0, array1, 0, a1.length);

    array2 = new long[a2.length];
    System.arraycopy(a2, 0, array2, 0, a2.length);

    mergeA(array1, array2);

  }

  static void mergeSort(long[] array){
    long[] helper = new long[array.length];
    mergeSort(array, helper, 0, array.length - 1);
  }

  static void mergeSort(long[] array, long[] helper, int low, int high){
    if (low < high){
      int middle = (low + high) / 2;
      mergeSort(array, helper, low, middle);
      mergeSort(array, helper, middle + 1, high);
      merge(array, helper, low, middle, high);
    }
  }

  static void merge(long[] array, long[] helper, int low, int middle, int high){
    for (int i = low; i <= high; i++){
      helper[i] = array[i];
    }

    int helperLeft = low;
    int helperRight = middle + 1;
    int current = low;
    while (helperLeft <= middle && helperRight <= high){
      if (helper[helperLeft] <= helper[helperRight]){
        array[current++] = helper[helperLeft++];
      }
      else{
        array[current++] = helper[helperRight++];
      }
    }
    while (helperLeft <= middle){
      array[current++] = helper[helperLeft++];
    }
  }


}
Here is your merged array: 
1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,