Home > Enterprise >  How to split an Array into 3 parts e.g int[] {8,3,6,2,9,5,1,7,4}. i am trying to write a mergesort m
How to split an Array into 3 parts e.g int[] {8,3,6,2,9,5,1,7,4}. i am trying to write a mergesort m

Time:12-20

just a check to see if the array is split the left array gets split but the centre has no elemnts in it. //i havent finished the code once i am able to split the array into three parts i can call the mergesort3 method to recursivley keep splitting it until there is no more items left to split. then i can merge them using merge3 method.just a check to see if the array is split the left array gets split but the centre has no elemnts in it. //i havent finished the code once i am able to split the array into three parts i can call the mergesort3 method to recursivley keep splitting it until there is no more items left to split. then i can merge them using merge3 method.


public static int[] mergesort3(int[] arr) {

    int elements = arr.length;
    int mod = elements % 3;
    int div = elements / 3;

    int sizeLeft = div   (mod > 0 ? 1 : 0);
    int sizeCenter = div  (mod > 0 ? 1 : 0);
    int sizeRight = div   (mod > 1 ? 1 : 0);
    int[] left = Arrays.copyOfRange(arr, 0, sizeLeft);
    int[] centre = Arrays.copyOfRange(arr, sizeLeft 1, sizeCenter);
    int[] right = Arrays.copyOfRange(arr, sizeCenter 1, sizeRight);

  

*****
but i am stuck on the first step of splitting this array into three different parts.
   //just to check if the array will print its values.
 for (int i = 0;i<left.length; i  ){
        System.out.println(centre[i]);
    }
//i get a illegal arugment exception.

    return merge3(left, centre, right);

i have tried searching online for help but i am struggling i would really appreciate if someone could point me in the right direction.

CodePudding user response:

copyOfRange expects the first index (inclusive) and the last index (exclusive) of the array.

Therefore passing sizeCenter and sizeRight as the last argument is wrong.

Therefore the splitting should be as follows:

int[] left = Arrays.copyOfRange(arr, 0, sizeLeft);
int[] centre = Arrays.copyOfRange(arr, sizeLeft, sizeLeft   sizeCenter);
int[] right = Arrays.copyOfRange(arr, sizeLeft   sizeCenter, arr.length);

From Javadoc:

int[] java.util.Arrays.copyOfRange(int[] original, int from, int to)

Parameters:

original the array from which a range is to be copied

from the initial index of the range to be copied, inclusive

to the final index of the range to be copied, exclusive.(This index may lie outside the array.)

P.S. you don't need a loop to print your arrays. Just use System.out.println (Arrays.toString (left));, etc...

CodePudding user response:

You can do via Java 8 :

int[] array = {8,3,6,2,9,5,1,7,4};

        int size = array.length / 3; // to get the size of 3 subarrays

        int[][] subarrays = IntStream.range(0, 3)
                .mapToObj(i -> Arrays.copyOfRange(array, i * size, (i   1) * size))
                .toArray(int[][]::new);

        for (int[] subarray : subarrays) {
            System.out.println(Arrays.toString(subarray));
        }
  • Related