0

BUBBLE SORT METHOD

public String bubbleSortCalc (float[] arr) {
 float[] b = deepCopy(arr);
 long start = System.nanoTime();
 boolean turnOn = false;
 do {
  turnOn = false;
  for (int i = 0; i < b.length - 1; i++) {
    if (b[i] > b[i + 1]) {
     swap(b, i, i + 1);
     turnOn = true;
    }
   }
  } while (turnOn);
 long end = System.nanoTime();

 return end - start + "ns";
}

INPUT ARRAY Element: Random float values | Length = 100 | Try: 3 times

BEST CASE AVERAGE WORST CASE
9300ns 316900ns 830800ns
6000ns 477300ns 684800ns
21000ns 1252800ns 1079600ns

I just don't understand how average case sometimes takes longer time than worst case. Thank you for answering my question!

I've tried with length 1000, 10000, 100000 also. I expected average case to be faster than worst case. Maybe something got in the way. And I tried to search for the answer but didn't find any hint.

bandaisme
  • 1
  • 1
  • "Try: 3 times" is nowhere near enough. Timing code is hard: see https://stackoverflow.com/questions/2842695/what-is-microbenchmarking – Andy Turner Dec 13 '22 at 17:14
  • This is, unfortunately, about how measuring the performance of Java is extremely hard and doing it what seems like the "obvious" way can produce outright lies. – Louis Wasserman Dec 13 '22 at 17:30

0 Answers0