從源碼看Java內建的排序sort()函數

來源:互聯網
上載者:User

標籤: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()函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.