Look at the Java built-in sort () function from the source code

Source: Internet
Author: User
Tags comparable

The General sort algorithm is a sort of array, and it uses random access methods. However, access to the list is inefficient. In fact, you can use merge sort to sort the list efficiently. The implementation of Java then is to simply transfer all the elements into an array, sort the arrays, and then copy the sorted sequence back to the list.

1 /**2 * Sorts the specified list according to the order induced by the3 * Specified comparator. All elements in the list must be <i>mutually4 * comparable</i> using the specified comparator (that's,5      * {@codeC.compare (E1, E2)} must not throw a {@codeClassCastException}6 * For any elements {@codeE1} and {@codeE2} in the list).7      *8 * <p>this sort is guaranteed to be <i>stable</i>: Equal elements 'll9 * Not being reordered as a result of the sort.Ten      * One * <p>the specified list must is modifiable, but need isn't be resizable. A      * - * @implNote - * This implementation defers to the {@linkList#sort (Comparator)} the * method using the specified list and comparator. -      * -      * @param<T> The class of the objects in the list -      * @paramlist the list to is sorted. +      * @paramc The comparator to determine the order of the list. A -      *        {@codeNULL} value indicates that the elements ' <i>natural + * ordering</i> should be used. A      * @throwsclasscastexception If the list contains elements that is not at * <i>mutually comparable</i> using the specified comparator. -      * @throwsunsupportedoperationexception If the specified list ' s - * List-iterator does not support the {@codeset} operation. -      * @throwsillegalargumentexception (optional) If the comparator is - * found to violate the {@linkComparator} contract -      * @seeList#sort (Comparator) in      */ -@SuppressWarnings ({"Unchecked", "Rawtypes"}) to      Public Static<T>voidSort (list<t> List, comparator<?SuperT>c) { + List.sort (c); -}
View Code

Look at the Java built-in sort () function from the source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.