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 8947511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:46:45+00:00 2026-06-15T12:46:45+00:00

I need help with using merge sort with a string array. I’ve seen many

  • 0

I need help with using merge sort with a string array. I’ve seen many examples of this with an int array but I need help using a string array. I can only use the .compareTo() method. This is my main method:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    String[] nameList = {"John", "Mark", "Amber", "Tony", "Matt", "George", 
    "Will", "Bob", "Paul", "Mary Ellen", "Kate", "Joe", "Fred", "Joe", 
    "Anne", "Amber", "Kimberly", "Kelsey", "Matthew"};

    //print original
    System.out.println("Before sorting the names: ");
    for(String element: nameList)
    System.out.print(element + " ");
    System.out.println("\n");
    //Merge Sort
    System.out.println("After sorting the names: ");
    mergesort(nameList, 0, nameList.length);

}

These are my methods:

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

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

    while(copied1 < n1)
        temp[copied++] = data[first + (copied1++)];

    for(int i = 0; i < copied; i++)
        data[first +i] = temp[i];

}


public static void mergesort(String[] data, int first, int n) {
    int n1 = 0;
    int n2 = 0;

    if(n > 1) {
        n1 = n/2;
        n2 = n-n1;

        mergesort(data, first, n1);
        mergesort(data, first + n1, n2);
    }

    merge(data, first, n1, n2);

    for(String element: data)
        System.out.print(element +" ");
}

When I run the program it sorts some of it correctly but over all it is not inorder.

  • 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-15T12:46:46+00:00Added an answer on June 15, 2026 at 12:46 pm

    You have one typo and two forgotten lines. Below is fixed function. Compare it with your one.

        private static void merge(String[] data, int first, int n1, int n2)
        {
            String[] temp = new String[n1 + n2];
            int copied = 0;
            int copied1 = 0;
            int copied2 = 0;
    
            while ((copied1 < n1) && (copied2 < n2))
            {
                if (data[first + copied1].compareTo(data[first + n1 + copied2]) < 0)
                    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 (int i = 0; i < copied; i++)
                data[first + i] = temp[i];
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help writing a script downloads data from google insight using c# this is
i need help with this. I'am using JQuery's Dynamic Form plug-in, it duplicates my
I really need help this time - Joomla 2.5 and latest K2. I'm using
I need to combine two video file using ffmpeg. Can you help me? I
Can anyone help with a SQL problem Im having whereby I need to merge
I need help with using VQmod, if anyone has any experience with, I'd appreciate
I need some help using regex in java. Im trying to achieve 2 different
Possible Duplicate: Regular expression: match all words except I need your help for using
We are using Linq-to-SQL with stored Procedures we need help on one of our
I need help reading the contents of a webpage. Currently i am using the

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.