Implementing custom sorting using the comparator interface in Java

Source: Internet
Author: User
Tags java comparator

In general, it is not a problem to write a simple sort program yourself, but can you guarantee the time complexity of the sequencing program you write? Can you guarantee the correctness of the program, robustness, and the clarity of the program structure, maintainability .... In summary, it is necessary to learn the sort interface to sort complex objects. Java has two to implement the sorting interface comparator and comparable interface, I prefer to use the Java comparator interface, in the program to implement the comparator interface compare (Object o1,object O2) method, which is then implemented in the program by calling Arrays.sort (Array,new DemoClass ()) or Collections.sort (Collection,new DemoClass ()) to sort the array of objects, Directly on a chestnut:

Describe:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Ideas:

Would like to imitate the method of merging two linked lists, find all the lists linked list to find a minimum value, and link the minimum value to the result list, and the minimum node is deleted, the minimum node is replaced by the next node, until null, the linked list is deleted.
But when Lists.size () is relatively large, time complexity comes up, and only one minimum node can be selected at a time, O (n*n) Rhythm ...
So, think of is not the sort, all of the nodes together, and then the quick sort of it is not okay, ah ha, really, bravo!
Obviously, this method is certainly not the optimal method, but the time complexity is only O (N*logn), is also acceptable.

Code:

public class Solution implements comparator<listnode> {public int compare (ListNode t1, ListNode T2) {return t1.val- T2.val;} Public ListNode mergeklists (list<listnode> lists) {if (lists = null | | lists.size () = = 0) return null;if (lists.size () = = 1)//Just have one Linkedlistreturn lists.get (0); int i=0,len=0; list<listnode> returnlist = new arraylist<listnode> ();  ListNode cur = null;for (i = 0; i < lists.size (); i++) {cur = lists.get (i); while (cur! = null) {returnlist.add (cur); cur = Cur.next;}} Collections.sort (returnlist, New Solution ()); ListNode pre = returnlist.get (0), cur = Null;len = Returnlist.size (), for (i = 1; i < Len; i++) {cur = returnlist.get (i); Pre.next = Cur;pre = cur;} Returnlist.get (len-1). Next = Null;return returnlist.get (0);}}



Using the comparator interface in Java for custom sorting

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.