简体   繁体   中英

How does Arrays.Sort work exactly?

I was looking at the source code here: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Arrays.java#Arrays.sort%28int%5B%5D%2Cint%2Cint%29

So, basically it uses a quicksort, but the base case is when the array is small enough to use an insertion sort (size < 7). If the size is equal to 7 it chooses the pivot as the middle element. If size is > 7 is when I get confused. I see that it gets the median if size is less than 40, but how does that med3 method work exactly? If size if bigger than 40 it becomes even more complicated.

How does the method find the median and what exactly does it do if size > 40?

If the size is greater than 7 but less than 40 it will use the median of the start, end and mid point as the pivot value.

If size > 40 it takes the pivot value to be the median of 9 values aproximately equally spaced within the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM