標籤:sorted public argument The col table 方式 view tee
一般排序演算法都是有關數組的排序,而且使用的是隨機訪問方式。但是對列表進行訪問的效率很低。實際上,可以使用歸併排序對列表進行高效的排序。然後Java的實現卻是:直接將所有元素轉入一個數組,對數組進行排序,然後再將排序後的序列複製回列表。
1 /** 2 * Sorts the specified list according to the order induced by the 3 * specified comparator. All elements in the list must be <i>mutually 4 * comparable</i> using the specified comparator (that is, 5 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 6 * for any elements {@code e1} and {@code e2} in the list). 7 * 8 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 9 * not be reordered as a result of the sort.10 *11 * <p>The specified list must be modifiable, but need not be resizable.12 *13 * @implNote14 * This implementation defers to the {@link List#sort(Comparator)}15 * method using the specified list and comparator.16 *17 * @param <T> the class of the objects in the list18 * @param list the list to be sorted.19 * @param c the comparator to determine the order of the list. A20 * {@code null} value indicates that the elements‘ <i>natural21 * ordering</i> should be used.22 * @throws ClassCastException if the list contains elements that are not23 * <i>mutually comparable</i> using the specified comparator.24 * @throws UnsupportedOperationException if the specified list‘s25 * list-iterator does not support the {@code set} operation.26 * @throws IllegalArgumentException (optional) if the comparator is27 * found to violate the {@link Comparator} contract28 * @see List#sort(Comparator)29 */30 @SuppressWarnings({"unchecked", "rawtypes"})31 public static <T> void sort(List<T> list, Comparator<? super T> c) {32 list.sort(c);33 }View Code
從源碼看Java內建的排序sort()函數