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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:48:00+00:00 2026-05-30T18:48:00+00:00

(Using Java) I’m testing sorting arrays to see literally how fast different sorting arrays

  • 0

(Using Java)

I’m testing sorting arrays to see literally how fast different sorting arrays take. I want to weed out the erroneous times, so ideally I would like to start a timer, run the sort in a loop of say 100 times, stop the timer, then divide by 100 to get a pretty accurate measure.

The problem is if I were to loop the same array, it’ll sort properly the first time, then each sort after, it’ll keep sorting the already sorted array, which isn’t what I want.

Maybe I’m missing an obvious solution, but is there any way I can make it keep sorting the same initial randomized array?

I thought about reassigning the newly sorted array back to the initial random array each time, but that would mess up my timer..

thanks for any suggestions

what i would like to do:

        startTime = System.nanoTime();
        for(int i=0; i<cntr; i++) {
            sort array
        }
        endTime = System.nanoTime();
        time = (endTime - startTime)/cntr;
  • 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-30T18:48:01+00:00Added an answer on May 30, 2026 at 6:48 pm

    You can make a copy before you start sorting, and then copy from that stored copy into the array being sorted in each iteration of the loop.

    int[] toBeSorted = new int[10000];
    // fill the array with data
    int[] copied = new int[10000];
    System.arrayCopy(toBeSorted, 0, copied, 0, copied.length);
    // prepare the timer, but do not start it
    for (int = 0 ; i != 100 ; i++) {
        System.arrayCopy(copied, 0, toBeSorted, 0, copied.length);
        // Now the toBeSorted is in its initial state
        // Start the timer
        Arrays.sort(toBeSorted);
        // Stop the timer before the next iteration
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java (1.6) I want to split an input string that has components of
When using java threads, one has to take care of the basic problems that
While using Java's switch case, it excepts only char and int, but I want
Using Java which is the best way to merge two arrays of class based
Using JAVA framework i want to achieve the following task. hot code(jar) deployment which
Using Java regex pattern I want to match the & symbol. But it should
When using Java LinkedList how do you find out the element's next or previous
I'm using java.util.zip to compress and uncompress byte arrays, using Inflater and Deflater .
using Java. the List is sorted so I want to use binary search. I'd
Using Java, I want to strip the fragment identifier and do some simple normalisation

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.