What different sorting algorithms are available in Java 6?

前端 未结 3 2047
清歌不尽
清歌不尽 2021-02-06 05:21

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 o

相关标签:
3条回答
  • 2021-02-06 05:41

    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 讨论(0)
  • 2021-02-06 05:58

    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.

    0 讨论(0)
  • 2021-02-06 05:58

    Arrays.sort(int[] a) uses a tuned quicksort.

    Arrays.sort[Object[] a) uses a modified mergesort.

    0 讨论(0)
提交回复
热议问题