0

I am working on a Merge-sort algorithm, and I am currently trying to test if it works.

It should be controlled through stdin with the input format:

array length element values (separated by " ")

However, when typing in:

3
17 10 3

Nothing happens, and hitting enter, just get me to the next line in the console.

I get no error message (unless I type in wrong input), and so I have a hard time figuring out why the script is not prompted to run with the input given.

(Code is copied below)

package merge;
import java.io.*;
import java.util.*;
public class MergeSort
{
 // This method takes two sorted arrays of integers as input parameters
 // and it should return one sorted array of integers.
 public int[] merge(int[] A1, int[] A2) {
 int[] C = new int[A1.length + A2.length];
 int i = 0, j = 0, k = 0;
 while (i < A1.length && j < A2.length)
 C[k++] = A1[i] < A2[j] ? A1[i++] : A2[j++];
 while (i < A1.length) 
 C[k++] = A1[i++];
 while (j < A2.length) 
 C[k++] = A2[j++];
 return C;
 }
 // This method takes an array of integers as input parameter,
 // and it should then return the integers sorted
 // in ascending order using the MergeSort algorithm.
 private int[] sort(int[] numbers) {
 //pointers
 int i = 0, j = 0, k = 0;
 // reference values
 int half = numbers.length / 2;
 int[] sorted = new int[numbers.length];
 if (numbers.length <= 1){
 return numbers;
 }
 //left part of 'numbers'
 int[] left = new int[half];
 int[] right;
 //right part of 'numbers' 
 if (numbers.length % 2 != 0){
 right = new int[half+1];
 j = half + 1;
 }
 else{
 right = new int[half];
 j = half;
 }
 //fills out left half of array with values from input array
 while (i < half)
 left[i] = numbers[i];
 i++;
 //fills out right half of array with values from input array
 while (j < numbers.length)
 right[j] = numbers[k];
 j++; k++;
 left = sort(left);
 right = sort(right);
 merge(left,right);
 return sorted;
 }
 // ##################################################
 // # Stdin part. #
 // ##################################################
 public static void main(String[] args) throws IOException {
 new MergeSort().run();
 }
 private void run() throws IOException {
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 int[] numbers = readIntArray(in);
 numbers = sort(numbers);
 for (int i = 0; i < numbers.length; i++) {
 System.out.print(numbers[i] + " ");
 }
 }
 private int[] readIntArray(BufferedReader in) throws IOException {
 int length = Integer.parseInt(in.readLine());
 int[] array = new int[length];
 StringTokenizer st = new StringTokenizer(in.readLine());
 for (int i = 0; i < length; i++) {
 array[i] = Integer.parseInt(st.nextToken());
 }
 return array;
 }
}
asked Feb 22, 2017 at 13:47

1 Answer 1

1

You call readLine() two times, resulting in an attempt to read two lines on the standard input.

answered Feb 22, 2017 at 14:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.