Java API algorithm

Source: Internet
Author: User

Algorithm

The polymorphic algorithm (polymorphic algorithms) described in this section is a reusable, functional fragment provided by the JDK. They are all taken from the collections class and all take the form of a static method (its first argument is the set of objects that perform the action). Most of the algorithms provided by the Java platform operate on the list object, but two (min and Max) operate on any collection object. Here is a description of the algorithm

Sort (sorting)

The sort algorithm can reorder a List so that its elements are sorted in ascending order according to some sort relationship. There are two forms of operation that are provided. The simple form of an operation takes only a List and sorts it according to the natural sort of its elements. If you are unfamiliar with the concept of natural ordering, you should re-read object ordering.

The sort operation uses the merge sort algorithm that makes some optimizations. If you don't know what it means and you value it, read any textbook on the algorithm. The important point of this algorithm is:

Fast: This algorithm is guaranteed to run in N log (n) time, and on a list that has been basically sorted, its speed is substantially faster. Experience has shown that its speed is almost the same as that of highly optimized fast sorting (quicksort), quicksort is generally considered faster than merge ordering, but it is unstable and does not guarantee n log (n) performance.

Stability: This means that it is not reordered for equal elements. This is important to you if you are repeating a different sort of attribute for the same list. If a user of a mail program sorts its mail box by date and then by sender, the user naturally expects that the current list of adjacent messages for a particular sender will (still) be sorted by date. This can only be ensured if the second ranking is stable.

The following is a small program that prints its parameters in a dictionary (alphabetical) Order:

import java.util.*;
public class Sort {
public static void main(String args[]) {
List l = Arrays.asList(args);
Collections.sort(l);
System.out.println(l);
}
}

Let's run this program:

% java Sort i walk the line
[i, line, the, walk]

This program is only meant to show that I have no reservations: the algorithm is indeed as simple as they appear. I don't want to underestimate your ability and demonstrate a more stupid example.

The second form of sort, in addition to a List, takes a Comparator and sorts the elements using Comparator. Remember in the Map course knot barium na Palmer Palmetto  Yong? It prints out the arrangement group in a neutral order. Let's say you want to print them in the opposite order, large in front. The following examples will show you how to use the second form of the sort method to achieve your purpose.

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.