开发者

What different sorting algorithms are available in Java 6?

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 i开发者_运维百科nto 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.


You generally don't get to choose (with the built in sorting, anyway). The Collections class provides a sort method which should be efficient enough for most needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜