Single-linked list sorting----Quick & Merge Sort

Source: Internet
Author: User
Tags prev sort

Title Description:
for the head node of a unordered single-linked list, sort the nodes in the linked list
require time complexity O (NLGN), Space complexity O (1)

analysis:
Because the topic requires time complexity i O (NLGN), so select sort and insert sort can be excluded.
in the sorting algorithm, the time complexity of O (NLGN) is mainly: merge sort, quick sort, heap sort.
The heap ordering has a space complexity of (n) and does not meet the requirements, so it can also be excluded.
when sorting on an array, a temporary array is required to store all the elements, and the spatial complexity is O (n). However, when a single-linked list is sorted using the merge algorithm, the relative position of the element can be recorded by the next pointer, so the time complexity is also O (1).
You can therefore consider using fast and merge to sort the single-linked list.

Quick sort:
The main idea of a quick sort is:
1) Select a Datum element
2) After a trip, divide all the elements into two parts
3) for two parts respectively Repeat until all elements have been sorted successfully
because the single-linked list can only be traversed backwards from the linked list head node, without the Prev pointer, you must select the head node as the datum element. The time complexity for this second operation is O (n). Since this is done separately for the two parts, the linked list is divided into LGN segments, so the time complexity of the O (nlgn)

diagram is as follows:


code:

    void swap (int *a,int *b) {int t=*a;
        *a=*b;
    *b=t; } ListNode *partion (ListNode *pbegin,listnode *pend) {if (pbegin==pend| |
        Pbegin->next==pend) return pbegin;    int key=pbegin->val;
        Select Pbegin as the datum element ListNode *p=pbegin,*q=pbegin;
                while (q!=pend) {//from pbegin start to traverse backward once if (Q->val<key) {p=p->next;
            Swap (&p->val,&q->val);
        } q=q->next;
        } swap (&p->val,&pbegin->val);
    return p; } void Quick_sort (ListNode *pbegin,listnode *pend) {if (pbegin==pend| |
        Pbegin->next==pend) return;
        ListNode *mid=partion (pbegin,pend);
        Quick_sort (Pbegin,mid);
    Quick_sort (Mid->next,pend); } listnode* sortlist (listnode* head) {if (head==null| |
        Head->next==null) return head;
        Quick_sort (Head,null);
    return head; }

  Merge Sort:
The merge sort is also based on the idea of divide and conquer, but unlike the fast one, the merge is divided first and then merged from the bottom up.
The main idea of merge sorting is to combine two already ordered segments into an ordered segment. In addition to finding an intermediate node, the operation must traverse the linked list, and the other operations are basically the same as the merge sort of the array.

  Code:  

    listnode* Merge_sort (listnode* head) {if (head = = NULL | | head->next = = NULL) return head;
        listnode* head1 = head;  listnode* head2 = Getmid (head);   Gets the middle node, divides the list into two segments Head1 = Merge_sort (HEAD1);
        The two-segment list is sorted separately head2 = Merge_sort (head2);  Return merge (Head1, head2); Merge two-segment ordered linked list} listnode* merge (listnode* head1, listnode* head2)//merge two ordered list {listnode* newhead = new L
        Istnode (-1);
        listnode* newtail = Newhead; while (Head1 && head2) {if (Head1->val <= head2->val) {NE
                Wtail->next = Head1;
            Head1 = head1->next;
                } else {newtail->next = head2;
            Head2 = head2->next;
            } Newtail = newtail->next;
        Newtail->next = NULL;
        } if (head1) Newtail->next = Head1; if (head2) newtail->next = head2;  Return newhead->next;
        Linked header node} listnode* getmid (listnode* head)//Get intermediate node and segment {listnode* fast = head->next;
        listnode* slow = head->next;
        listnode* prev = head;
            while (true) {if (fast = = NULL) break;
            Fast = fast->next;
            if (fast = = NULL) break;

            Fast = fast->next;
            prev = slow;
        slow = slow->next;  } prev->next = NULL;
    The linked list is divided into two sections return slow; }

So the key to merge sorting is to turn the single-linked list into two sorted segments. The
Getmid schematic is as follows:

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.