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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:09:00+00:00 2026-05-31T11:09:00+00:00

I wrote a program to test and verify the running time of insertion sort

  • 0

I wrote a program to test and verify the running time of “insertion sort” which should be O(n^2). The output doesn’t look right to me and it doesn’t seem to vary much between different runs. The other odd thing is that the second time through is always the smallest. I expect there to be greater variance every time I run the program but the run times don’t seem to fluctuate as much as I would expect. I’m just wondering if there are some kind of optimizations or something being done by the JVM or compiler. I have similar code in C# and it seems to vary more and the output is as expected. I am not expecting the running times to square every time but I am expecting them to increase more than they are and I certainly expect a much greater variance at the last iteration.

Sample Output (it doesn’t vary enough for me to include multiple outputs):

  • 47
  • 20 (this one is ALWAYS the lowest… it makes no sense!)
  • 44
  • 90
  • 133
  • 175
  • 233
  • 298
  • 379
  • 490

    public class SortBench {
    
    public static void main(String args[]){
    
        Random rand = new Random(System.currentTimeMillis());
    
        for(int k = 100; k <= 1000; k += 100)
        {
            //Keep track of time
            long time = 0;
            //Create new arrays each time
            int[] a = new int[k];
            int[] b = new int[k];
            int[] c = new int[k];
            int[] d = new int[k];
            int[] e = new int[k];
    
            //Insert random integers into the arrays
            for (int i = 0; i < a.length; i++)
            {
                int range = Integer.MAX_VALUE;
                a[i] = rand.nextInt(range);
                b[i] = rand.nextInt(range);
                c[i] = rand.nextInt(range);
                d[i] = rand.nextInt(range);
                e[i] = rand.nextInt(range);
            }
            long start = System.nanoTime();
            insertionSort(a);
            long end = System.nanoTime();
            time += end-start;
    
            start = System.nanoTime();
            insertionSort(b);
            end = System.nanoTime();
            time += end-start;
    
            start = System.nanoTime();
            insertionSort(c);
            end = System.nanoTime();
            time += end-start;
    
            start = System.nanoTime();
            insertionSort(d);
            end = System.nanoTime();
            time += end-start;
    
            start = System.nanoTime();
            insertionSort(e);
            end = System.nanoTime();
            time += end-start;
    
            System.out.println((time/5)/1000);
        }
    }
        static void insertionSort(int[] a)
        {
            int key;
            int i;
            for(int j = 1; j < a.length; j++)
            {
                key = a[j];
                i = j - 1;
                while(i>=0 && a[i]>key)
                {
                    a[i + 1] = a[i];
                    i = i - 1;
                }
                a[i + 1] = key;
            }
        }
    }
    
  • 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-31T11:09:01+00:00Added an answer on May 31, 2026 at 11:09 am

    Warm up the JVM’s JIT optimization of your function, memory allocators, TLB, CPU frequency, and so on before the timed region.

    Add some untimed calls right after seeding the RNG, before your existing timing loop.

    Random rand = new Random(System.currentTimeMillis()); 
    
    // warmup
    for(int k = 100; k <= 10000; k += 100) 
    {
        int[]w = new int[1000];
        for (int i = 0; i < w.length; i++) 
        { 
           int range = Integer.MAX_VALUE; 
           w[i] = rand.nextInt(range); 
           insertionSort(w);
         }
     }
    

    Results with warming:

    4
    16
    27
    47
    68
    97
    126
    167
    201
    250
    

    Results without warming:

    62
    244
    514
    206
    42
    59
    80
    98
    122
    148
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a test program to monitor my Picture folder which points to c:\users[username]\Pictures
I wrote a test program thinking that the address of p1 will be less
I wrote a small program to test accessing a widget parent's slot. Basically, it
i wrote a little test programm which queries the status servlet of a tomcat
I wrote a program which includes writing and reading from database. When I run
I wrote a program out, which was all in one file, and the methods
Here's a small test program I wrote: #include <iostream> using namespace std; class A
I'm learning to work with lucene. I wrote a simple program to test lucene
I wrote a test program wherein a single Button is defined in XAML as
I wrote the following program to test when the copy constructor is called and

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.