Aki K
Aki K

Reputation: 1262

3-way mergesort

I have as an assignment to implement something like a 3-way mergeshort in java. I will have as an input an array of integers and I have to do the following:

Implement mergesort in arrays with a 3-way division and also print the 3 sorted partitions of the array. For example if I have as an input the following array [9 23 10 90 70 10 3 23] the output would be firstly the 3 partitions sorted: [9 10 24] [70 90] [3 10 23] and then the final array sorted [3 9 10 10 23 24 70 90].

This is what I have implemented so far:

public static void mergesort(int[] data) {
    int elements = data.length - 1;
    int length1;
    int length2;
    int length3;

    if (elements % 3 == 0) {
        length1 = elements / 3;
        length2 = elements / 3;
        length3 = elements / 3;
    } else if (elements % 3 == 1) {
        length1 = (elements / 3) + 1;
        length2 = elements / 3;
        length3 = elements / 3;
    } else { //if (elements % 3 == 2)
        length1 = (elements / 3) + 1;
        length2 = elements / 3;
        length3 = (elements / 3) + 1;
    }

    Arrays.sort(data, 0, length1 - 1);
    Arrays.sort(data, length1, length1 + length2 - 1);
    Arrays.sort(data, length1 + length2, length1 + length2 + length3 - 1);

    merge(data, 0, length1, length1 + length2);
    merge(data, 0, length1 + length2, length1 + length2 + length3);
}

private static void merge(int[] data, int first, int n1, int n2) {
    int[] temp = new int[n1 + n2];
    int copied = 0;
    int copied1 = 0;
    int copied2 = 0;
    int i;

    while ((copied1 < n1) && (copied2 < n2)) {
        if (data[first + copied1] < data[first + n1 + copied2]) {
            temp[copied++] = data[first + (copied1++)];
        } else {
            temp[copied++] = data[first + n1 + (copied2++)];
        }
    }

    while (copied1 < n1) {
        temp[copied++] = data[first + (copied1++)];
    }
    while (copied2 < n2) {
        temp[copied++] = data[first + n1 + (copied2++)];
    }

    for (i = 0; i < n1 + n2; i++) {
        data[first + i] = temp[i];
    }
}

What I've done is first of all split the array in 3 parts depending the circumstances, after that I sort the 3 parts of the array and later I try merging the first 2 parts and then the combined part with the last part.

I have implemented these 2 methods for starters but first of all I'm pretty confident that the merge method is awful and wrong and second of all I believe something is wrong with my approach in this problem I feel that even the mergesort method is wrong and awfully implemented.

What I want is advice as to what I should be doing in this problem and what is totally wrong with my implementation

Upvotes: 2

Views: 8251

Answers (1)

noMAD
noMAD

Reputation: 7844

void merge(int arr1[], int arr2[])
{
  int p1 = 0;
  int p2 = 0;
  int arr3[] = new int[arr1.length + arr2.length];

  while(p1 < arr1.length && p2 < arr2.length)
  {
    if(arr1[p1] > arr2[p2])
    {
      arr3[p2] = arr2[p2];
      p2++;
    }
    else
    {
      arr3[p1] = arr1[p1];
      p1++;
    }
  }

  //Now just add the code for just concatenating any remaining elements in 
  // either arr1 or arr2
  //This will happen if the lengths of arr1 and arr2 differ 


}

This is the basic code for merging two arrays (Not tested, might miss boundary conditions). Try to incorporate it to your code.

Upvotes: 2

Related Questions