The are several sorting algorithms like inserstion sort, selection sort, bubble sort etc. that are often discussed in computer science textbooks. Given an array of integers or objects, are there built-in Java 6 language API that let me choose to apply a specfic sort algorithm to sort the array instead of me reinventing these wheels again? If not built into Java 6, are there open source libraries that prodivde this functionality and what are they?
The Arrays.sort()
methods use a quick sort in all primitive type arrays.
The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
The Collections.sort()
method uses a merge sort. This sort is also used in Arrays.sort(Object[])
and Arrays.sort(T[], Comparator<? super T>)
.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
Arrays.sort(int[] a)
uses a tuned quicksort.
Arrays.sort[Object[] a)
uses a modified mergesort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With