0

I want to see how long it takes for 10,000 random integers to be sorted. Since in a bubblesort, the arrays are sorted at each stage and it could also vary each time, I want to know the total time it takes for the final sorting array to appear. So my time calculations should be when each sorting of the array is taking place, and when the final sorting happens and the results appear, the output should tell me the time in seconds.

I have used System.currentTimeMillis(); for this task but how would I use it so it calculates the time at each sorting stage? I have used it inside the for (int k = 0; k < numbers.length; k++){ loop because this loops through all the stages of the sorting, but my program would not output anything. How would I fix that?

Code:

class Main {
public static void main(String[] args) {

// Clear screen
System.out.print("\033[H\033[2J");
System.out.flush();

double msStartTime = 0d;
double msEndTime = 0d;

// Initialize an int array variable and set the limit to 10,000
int numbers[] = new int[10000];

// Generate random 10,000 integers to bubblesort
for (int x = 0; x < numbers.length; x++) {
  numbers[x] = (int) (Math.random() * 10001);
}

for (int i = 0; i < numbers.length; i++) {
  for (int j = i; j < numbers.length; j++) {
    if (numbers[j] < numbers[i]) {
      int temp = numbers[j];
      numbers[j] = numbers[i];
      numbers[i] = temp;
    }
  }
  for (int k = 0; k < numbers.length; k++){
    msStartTime = (double) System.currentTimeMillis();
  }
}
msEndTime = (double) System.currentTimeMillis();

System.out
    .println("To sort an array of 10,000 integers, it takes " + (msEndTime - msStartTime) / 1000 + " seconds");
  }
}
  • For starters, I would initialize `msStartTime` only once, before the `for (int i...` loop. Then it could measure the time for sorting the entire thing, probably still very close to 0, as 10000 elements are not too many. – tevemadar Oct 20 '20 at 22:03
  • I did that and got 0.695 seconds as my output, but my question is did it calculate that time-based to only the sorting stage of each array? –  Oct 20 '20 at 22:23
  • So 0.695 is the time it took to for each of the sorting of the stage to happen? –  Oct 20 '20 at 22:30
  • I think the document I point to in this answer should be a helpful read if you want to get a better idea of how to do this kind of (micro)benchmarking: https://stackoverflow.com/a/43004367/1214974 – Janus Varmarken Oct 21 '20 at 02:47
  • If the `msStartTime =...` is located at the suggested position, then yes, 0.695 seconds measured the time spent in the i-j loop-pair, so the sorting itself. However you should take into account that Java code speeds up as it runs, so you may want to wrap the entire contents of the `main` into a loop and run it, say, 10 times. Then you will probably see that later runs are faster, perhaps even the second one already. – tevemadar Oct 21 '20 at 07:48

1 Answers1

0

i think you can use StopWatch.here is how u can add it to maven and use it https://www.baeldung.com/java-measure-elapsed-time

DaniyalVaghar
  • 138
  • 1
  • 2
  • 9