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

  • SEARCH
  • Home
  • 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 6877933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:39:40+00:00 2026-05-27T04:39:40+00:00

from wiki page of insertion sort: Some divide-and-conquer algorithms such as quicksort and mergesort

  • 0

from wiki page of insertion sort:

Some divide-and-conquer algorithms such as quicksort and mergesort sort by recursively dividing the list into smaller sublists which are
then sorted. A useful optimization in practice for these algorithms is
to use insertion sort for sorting small sublists, where insertion sort
outperforms these more complex algorithms. The size of list for which
insertion sort has the advantage varies by environment and
implementation, but is typically between eight and twenty elements.

the quote from wiki has one reason is that, the small lists from merge sort are not worse case for insertion sort.

I want to just ignore this reason.

I knew that if the array size is small, Insertion sort O(n^2) has chance to beat Merge Sort O(n log n).

I think(not sure) this is related to the constants in T(n)

Insertion sort: T(n) = c1n^2 +c2n+c3

Merge Sort: T(n) = n log n + cn

now my question is, on the same machine, same case (worse case), how to find out the largest element number, let insertion sort beat merge sort?

  • 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-05-27T04:39:41+00:00Added an answer on May 27, 2026 at 4:39 am

    It’s simple:

    Take a set of sample arrays to sort, and iterate over a value k where k is the cutoff point for when you switch from merge to insertion.

    then go

    for(int k = 1; k < MAX_TEST_VALUE; k++) {
        System.out.println("Results for k = " + k);
        for(int[] array : arraysToTest) {
            long then = System.currentTimeMillis();
            mergeSort(array,k); // pass in k to your merge sort so it uses that
            long now = System.currentTimeMillis();
            System.out.println(now - then);
        }
    }
    

    For what it’s worth, the java.util.Arrays class has this to say on the matter in its internal documentation:

    /**
     * Tuning parameter: list size at or below which insertion sort will be
     * used in preference to mergesort or quicksort.
     */
    private static final int INSERTIONSORT_THRESHOLD = 7;
    
    /**
     * Src is the source array that starts at index 0
     * Dest is the (possibly larger) array destination with a possible offset
     * low is the index in dest to start sorting
     * high is the end index in dest to end sorting
     * off is the offset to generate corresponding low, high in src
     */
    private static void mergeSort(Object[] src,
                  Object[] dest,
                  int low,
                  int high,
                  int off) {
        int length = high - low;
    
        // Insertion sort on smallest arrays
        if (length < INSERTIONSORT_THRESHOLD) {
            for (int i=low; i<high; i++)
                for (int j=i; j>low &&
             ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
                    swap(dest, j, j-1);
            return;
        }
    

    In its primitive sequences, it also uses 7, although it doesn’t use the constant value.

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

Sidebar

Related Questions

I installed wxWidgets-2.8.10 following instructions from this page: http://wiki.wxwidgets.org/Compiling_and_getting_started Libraries directory: alex@alex-linux:/usr/local/lib$ ls |
i have such code (copy paste from wiki). Its multiplication of those big numbers
I'm trying to remove wiki formatting from some text so it can be parsed.
for(; max != 0 ; max/=10, pow10*=10) Pulled this exerpt from the wiki page
I installed PhoneGap 0.9.2 and followed the instructions from the PhoneGap wiki page Getting
I want to get a content page from Wiki in this format (the picture
From the greasemonkey wiki page, there is an example using jQuery with greasemonkey. The
So the example from the wiki has phantom.exit() in two places here. Why can't
From the Mojolicious wiki on the topic of Database support: Although SQL::Abstract looks like
The Issue I'm migrating wiki pages from the FlexWiki engine to the FOSwiki engine

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.