How can I implement this pseudocode in java?

0

Good day for everyone I hope you are very well.

My question is this, I am trying to implement this pseudocodigo in java programming language:

But the truth is I'm very new to issues of using recursion, I've tried in many ways but I really can not do it recursively, for the moment I've done this:

public class MergeSort 

{

public static void main(String[] args) 
{
    int unsorted_array[]= {8,9,3,4,2,1};
    int sorted_array[]=new int [unsorted_array.length];

    int MergeSort (int unsorted_array[], int n,int left_sub_array,int right_sub_array[]) 
    {
        if(unsorted_array.length==1) 
            return unsorted_array[n];

        else {
            //split in two parts
            left_sub_array=unsorted_array[0]/unsorted_array[n/2];
            right_sub_array=unsorted_array[(unsorted_array.length/2)+1]=unsorted_array[unsorted_array.length];
             }
        return left_sub_array;

    }

    }
}

I add that I have to use a function that reads the numbers by the user separated by a comma and I know that it is as follows:

String[] numeros=br.readLine().split(",");
int[] array = new int[numeros.length];

for(int i = 0; i < numeros.length; i++) {
array[i] = Integer.parseInt( numeros[i] );
}

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

but as I said, I would not know where to put this function in a recursive algorithm.

I would really appreciate if you could give me a help, thank you very much.

    
asked by Yesith 30.11.2017 в 13:48
source

1 answer

1

I understand that a classmate will respond with the complete answer, but for my part I am not a fan of solving the didactic exercises exposing the answer directly, since the purpose of this task is for you to learn as Recursion is performed.

What I will tell you is that a function is recursive because it calls itself. Within your MergeSort method you should call MergeSort again, in fact this is indicated in the pseudocode in the lines:

SortedL <- MergeSort(left-subarray) 
SortedR <- MergeSort(right-subarray)

Also tell you that the MergeSort method only accepts one input parameter according to the exercise and you put 3. It should be:

int MergeSort (int unsorted_array[]) 

In the end, what you are doing is taking the array, dividing it into two parts and ordering the numbers one by one by always calling the same function until, as indicated in the pseudocode, your array only has one number (dot stop the recursion). Once this is done, you unite them with another function called Merge.

And one last thing, depending on how you've put the code, you're putting MergeSort inside the Main function. The method must be left out of this function. I expose a basic scheme:

public class EjercicioMerge
{

    public static void main(String[] args) 
    {
        int unsorted_array[]= leerArray();
        int sorted_array[]= mergeSort(unsorted_array);

        ...
    }

    static  int mergeSort (int unsorted_array[]) 
    {

        .....

    }

    static  int[] leerArray()
    {
        .....
    }

}
    
answered by 30.11.2017 в 14:51