Leetcode sort list

Source: Internet
Author: User
Sort a linked list in O (n log n) time using constant space complexity. /*** definition for singly-linked list. * struct listnode {* int val; * listnode * Next; * listnode (int x): Val (x), next (null ){}*}; */class solution {public: listnode * sortlist (listnode * head) {If (! Head) return NULL; int K = 0; listnode * P = head; while (p) {k ++; P = p-> next;} If (k = 1) return head; int L = K/2; // calculate the number of nodes and separate the linked list. P = head; listnode * q = head, * t = NULL; For (INT I = 0; I <L & Q; I ++) {if (I = L-1) t = Q; q = Q-> next;} If (t) T-> next = NULL; // separate the linked list. P = sortlist (p); // sort the two segments respectively q = sortlist (Q); listnode * Hd = NULL, * PP = NULL; while (P & Q) // merge {If (p-> Val <= Q-> Val) {If (! HD) pp = Hd = P; else {PP-> next = P; pp = P;} p = p-> next;} else {If (! HD) Hd = pp = Q; else {PP-> next = Q; pp = Q;} q = Q-> next ;}} if (P) PP-> next = P; If (q) PP-> next = Q; return HD ;}};

The analysis is as follows:
An interesting question. On the surface, the algorithms with O (N lgn) time complexity are fast sorting, heap sorting, and Merge Sorting. The spatial complexity of the three algorithms is O (1 ), O (N), O (N)
So at the beginning, I tried to solve the problem by using a fast sorting method, but I found that the code was hard to write. So I read the prompt online and found that the method was incorrect. The Merge Sorting method should be used.

In general, for arrays, the space complexity of merging and sorting is O (n). You need to open an extra space of O (n) to accommodate arrays, to indicate the order after merging. However, for the linked list, you can save the overhead of this part of space. You only need to change the next pointer of the node to indicate the new order after merging, so the space complexity suddenly drops to O (1)

 

Summary:

(1) An interesting question. You need to think about whether the old conclusion is still true under the new conditions and environment.

(2) the time complexity of fast sorting and Merge Sorting is O (n lgn), but CLRS says, practice has proved that fast sorting is faster than Merge Sorting. Why? In addition, this conclusion has a limited scope. This conclusion applies when sorting arrays. Why is the speed of merging and sorting better than that of quick sorting for linked lists? The comparison here is quite good. Copy it directly.

One of the main sources of efficiency in quicksort is locality of reference, where the computer hardware is optimized so that Accessing memory locations that are near one another tends to be faster than Accessing memory locations scattered throughout memory. the partitioning step in quicksort typically has excellent locality, since it accesses consecutive array elements near the front and the back. as a result, quicksort tends to perform much better than other sorting algorithms like heapsort even though it often does roughly the same number of comparisons and SWAps, since in the case of heapsort the accesses are more scattered.

Additionally, quicksort is typically much faster than other sorting algorithms because it operates in-place, without needing to create any auxiliary arrays to hold temporary values. compared to something like merge sort, this can be a huge advantage because the time required to allocate and deallocate the auxiliary Arrays can be noticeable. operating in-place also improves quicksort's locality.

When working with linked lists, neither of these advantages necessarily applies. because linked list cells are often scattered throughout memory, there is no locality bonus to accessing adjacent linked list cells. consequently, one of quicksort's huge performance advantages is eaten up. similarly, the benefits of working in-place no longer apply, since merge sort's linked list algorithm doesn't need any extra auxiliary storage space.

That said, quicksort is still very fast on linked lists. merge sort just tends to be faster because it more evenly splits the lists in half and does less work per iteration to do a merge than to do the partitioning step.

 

To sum up, it means that if the elements to be sorted are stored in the array, there are two reasons for faster sorting compared to merge sorting. First, you can quickly read elements (compared with the linked list, elements in the array are placed sequentially, while elements in the linked list are randomly placed ), the partion of the array is faster than the partion of the linked list. Second, in the merge stage, the Merge Sorting requires an auxiliary array and an O (n) space. It also takes time to apply for the space. Fast sorting does not require additional application space. If the elements to be sorted are stored in the linked list, the advantage of fast sorting becomes a disadvantage. Merging and sorting results in better speed.

Leetcode sort list

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.