1

I am trying to sort two arrays into one array. But I am having a few problems. it's not sorting correctly. I have attached the files, code and the output.

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);


}

}

input 1 2 4 6 8 10

input 2 12 14 16 18 20 22 24

output Input filename: input1_1.txt 2 4 6 8 10

Please enter your second file name: 
input1_2.txt
   12 14 16 18 20 22 24 
Here is your merged array: 
2, 4, 6, 8, 10, 0, 0, 0, 0, 0, 0, 0,
isabella
  • 151
  • 4
  • 15
  • When you declare array as `int[20]` you get an array filled with 20 zeroes. – PM 77-1 Apr 24 '15 at 02:34
  • 1
    Doesnt the `merge(int[], int[])` method of a merge-sort merge two sorted arrays? You need to read up on merge sort i think. And find a debugger – D. Ben Knoble Apr 24 '15 at 02:34
  • but my professor said to set all arrays to the size of 20....is there a way to go around this? – isabella Apr 24 '15 at 02:35
  • Merge sort doesnt merge 2 arrays. It splits up an original array and then sorts it by merging it back together in the correct order (bad explanation i know). Basically you don't start off with 2 arrays when using the merge sort you start with one. – yitzih Apr 24 '15 at 02:36
  • my professor said to merge two arrays into one array that is in ascending order. and then have the third hold both of them in order. – isabella Apr 24 '15 at 02:44
  • You could keep a index of the last entry, and only print up to that index. – Daniel Nugent Apr 24 '15 at 03:08
  • Are you sure that you are supposed to be using non-sorted arrays as input? – Daniel Nugent Apr 24 '15 at 03:42
  • some will be sorted and some not. it is a text file. So I just decided to sort them all just in case. but Now the problem is that it doesn't like numbers above 12. it replaces them with zeros... – isabella Apr 24 '15 at 03:46
  • The standard merge sort algorithm is for merging two sorted arrays into one sorted array. Merging two non-sorted arrays into one sorted array cannot use the merge algorithm from merge-sort. – Daniel Nugent Apr 24 '15 at 03:49
  • okay. I sorted them though. The problem now is that it does not like numbers above 12. It replaces them with zeros. – isabella Apr 24 '15 at 03:56
  • It doesn't work for your initial input, since that was un-sorted. For merging two sorted arrays, see this: http://stackoverflow.com/questions/5958169/how-to-merge-two-sorted-arrays-into-a-sorted-array – Daniel Nugent Apr 24 '15 at 04:44

1 Answers1

0

If you really have the requirement of having un-sorted arrays as potential input, you could use Merge Sort to sort the two individual arrays, and then do one last Merge operation to join the two sorted arrays.

Here is a test run with your initial non-sorted input:

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++];
    }
  }


}

Output:

Here is your merged array: 
1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137