Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:14:34+00:00 2026-06-04T02:14:34+00:00

I have as an assignment to implement something like a 3-way mergeshort in java.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:14:35+00:00Added an answer on June 4, 2026 at 2:14 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement Java.Polynomial as a school assignment. Part of the methods are
I have an assignment to implement ray tracing in Java. I'm not asking for
Hi I have a homework assignment where I need to implement an intersection of
I have to implement a search algorithm for a school assignment. Right now, i'm
I'd like to use the following idiom, that I think is non-standard. I have
I have an assignment to implement my own version of Collections.fill() and Collections.reverse(). The
for a programming assignment I have been asked to implement a solution to the
I want to have something like this below: template <class T> struct Container{ public:
I have an assignment that I'm supposed to implement the MIPS processor in C++
I have a Assignment question about Java Generics. The given class is Product.java. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.