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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:38:14+00:00 2026-06-13T15:38:14+00:00

I have written an in place mergesort algorithm for sorting a large set of

  • 0

I have written an in place mergesort algorithm for sorting a large set of data of random size (100,000 elements or more). I was thinking about putting in insertion sort for when the data is almost sorted to make the algorithm run a little bit faster. I was wondering if this is possible with in place mergesort?

Here is some of my code.

public static void merge(ArrayList<String> list, int low, int high) {
   if (low < high) {
        int mid = (low + high) / 2;
        merge(list, low, mid);
        merge(list, mid + 1, high);
        mergeSort(list, low, mid, high);
    }

}

public static void mergeSort(ArrayList<String> list, int first, int mid,
        int last) {
    int left = first;
    int right = mid + 1;
    String holder = "";

    // if mid <= mid+1 skip merge
    if (compareTo(list.get(mid), list.get(right)) <= 0) {
        return;
    }

    while (left <= mid && right <= last) {
        // if left index <= right index then just add to left
        if (compareTo(list.get(left), list.get(right)) <= 0) {
            left++;
        } else {
            holder = list.get(right);
            copyList(list, left, right - left);//moves everything from left to right-left                       up one index in the arraylist
            list.set(left, holder);

            left++;
            mid++;
            right++;
        }
    }
    // what is left is in place

}

public static void copyList(ArrayList<String> source, int srcPos, int length) {
    String temp1 = "";
    String temp2 = source.get(srcPos);
    for (int i = 0; i < length; i++) {
        temp1 = source.get(srcPos + 1);
        source.set(srcPos + 1, temp2);
        temp2 = temp1;
        srcPos++;
    }
}

Now, I was thinking of implementing Insertion sort by counter the number of elements when I first throw them into the arraylist and then changing my merge method to the following.

public static void merge(ArrayList<String> list, int low, int high) {
   if(high-low==dataSize-1){
        int mid = (low + high) / 2;
        merge(list, low, mid);
        merge(list, mid + 1, high);
        insertionSort(list);
   }else if (low < high) {
        int mid = (low + high) / 2;
        merge(list, low, mid);
        merge(list, mid + 1, high);
        mergeSort(list, low, mid, high);
    }

}

However, this actually makes my algorithm to take an eternity. Im guessing I’m doing this wrong and the algorithm is taking n^2 to run since the data is completely randomly generated and no where close to almost sorted.

What am I doing wrong? Any suggestions? My guess is since its in place merge-sort it wont work.

Thanks!

  • 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-13T15:38:16+00:00Added an answer on June 13, 2026 at 3:38 pm

    Such algorithms are complicated and easy to get wrong. I implemented something very similar: an in-place stable merge sort. It also uses insertion sort for small sub-lists. I suggest to have a look at the source code and compare it with what you are doing. You might also be interested in a in-place stable quicksort.

    Unless I’m mistaken your implementation is not stable (it might re-arrange elements that are equal). Depending on the use case, this may or may not be a problem.

    Also, it seems your implementation is O(n^2) because the copyList method is O(n) and it is called n times.

    About the insertionSort: what is dataSize and why do you compare it using equals? Don’t you want to use < instead? If you do, the else if (low < high) is redundant (it is always true).

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

Sidebar

Related Questions

I have written a maven mojo that does in-place JavaFlow instrumentation of a set
I have written a function that sorts a big scale of data. To test
I have written several scripts for my hudson builds. I have place them in
I have written one MapActivity class that is able to display a set of
I have written R code that merges two data frames based on first column
I have some PVRTC 4bpp image data that needs to be flipped vertically in-place
I have written test code as bellow: Entities db = new Entities(); var place
I have written a large module in F# that happens to have a trivial
I have written the following code to place the image path into sql server
I have written a script that checks a set of radiobuttons to be checked.

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.