0

In Java 8, function Arrays.sort() depends on the length of array;

if(length>=`QUICKSORT_THRESHOLD=286`){
    take `Dual-Pivot Quicksort`;
}
else if(length<`QUICKSORT_THRESHOLD=286` && length>`INSERTION_SORT_THRESHOLD=47`){
    take `One-Pivot Quicksort
}
else { take `Insertion Sort`}

How does the 286 or 47 comes from?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Tangoo
  • 1,329
  • 4
  • 14
  • 34

2 Answers2

1

This is based on the calculation of the complexity of the algorithm.

Some infos here on StackOverflow: How to optimize quicksort

Merge sort: https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/analysis-of-merge-sort

Quick sort: https://www.khanacademy.org/computing/computer-science/algorithms/quick-sort/a/analysis-of-quicksort

Community
  • 1
  • 1
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
1

Insertion sort is faster than quick sort for smaller arrays because there are less constant-factor overheads involved. Same with the single-pivot vs dual-pivot. We need to find out at what point this is to get these numbers.

They probably tried a range of numbers and then ran performance tests for each of them, and kept the numbers with the best performance.

fgb
  • 18,439
  • 2
  • 38
  • 52