Collections vs Arrays regarding sort()

前端 未结 8 2016
北荒
北荒 2021-01-31 03:20

Collections vs Arrays regarding sort() What is the difference between these two regarding sort() method? I know Arrays\' sort() is using binary search for sort(), what about Co

8条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 04:03

    Class Arrays

    public static void sort(T[] a)

    Parameters:
    a - the array to be sorted

    Implementation note (truncated):

    1. Dual-Pivot Quicksort and offers O(n log(n)) performance on many data sets.
    2. Faster than traditional (one-pivot) Quicksort implementations.

    Class Collections

    public static > void sort(List list)

    Parameters:
    list - the list to be sorted.

    Implementation note (truncated):

    1. Is a mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered.
    2. If the input array is nearly sorted, the implementation requires approximately n comparisons.
    3. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

    Summation

    1. They are based on different types of data with one in Array and another in List.
    2. When concerning the time complexity, it depends on the data is partially sorted or randomly sorted
    3. When concerning the space, Arrays.sort requires constant time, but Collections.sort may take up to n/2 space.

提交回复
热议问题