Here is how the Oracle JVM 1.7 sorts data:
Arrays.sort(Object[]) uses TimSort,
Arrays.sort on native arrays uses a dual-pivot quicksort,
Collections.sort dumps the list into an Object[] array, sorts the array with TimSort, and then re-adds elements to the list from the array.
To sort parallel arrays
To avoid useless object allocations: to sort a random-access list (such as ArrayList, but not LinkedList), there is no need to dump all your elements into an array, you could instead sort the list in place and save memory.
To better fit your data: if your data is “almost” sorted, there is a good chance that TimSort would perform faster than Quicksort.
To better fit your constraints
To better reuse memory
Read full article from Versatile sorting - Adrien Grand
Arrays.sort(Object[]) uses TimSort,
Arrays.sort on native arrays uses a dual-pivot quicksort,
Collections.sort dumps the list into an Object[] array, sorts the array with TimSort, and then re-adds elements to the list from the array.
To sort parallel arrays
To avoid useless object allocations: to sort a random-access list (such as ArrayList, but not LinkedList), there is no need to dump all your elements into an array, you could instead sort the list in place and save memory.
To better fit your data: if your data is “almost” sorted, there is a good chance that TimSort would perform faster than Quicksort.
To better fit your constraints
To better reuse memory
Read full article from Versatile sorting - Adrien Grand
No comments:
Post a Comment