Quick sort of single-linked list

Source: Internet
Author: User

The quick line does not contract in the list, but can be achieved, the time complexity of O (NLGN)

Average Time complexity O (NLOGN), Space complexity is O (1) regardless of recursive stack space

Analysis: Because the single-linked list does not have a prev pointer, the same Low,high pointer as an array is not suitable for single-linked lists

Method One: do not move the element node itself, only move the value of the element //Note must take the first element as the Datum pivot value,

1) Define two pointer pslow,pfast, where pslow points to the head node of the single-linked list, Pfast points to the next node of the single-linked header node;

2) Pslow represents the tail node of a list that is smaller than the base node value, all nodes of this list are smaller than the base value, Pslow is the tail node

3) nodes after Pslow are nodes that are larger than the base node value

4) Use Pfast to traverse the single-linked list, if found at this time pfast node value Pfast->value < Pivot, then Pslow=pslow->next, this time the Pslow is greater than the node of the reference node, Swap (Pslow,pfast), which is less than the node of the Datum node, in Pslow, because the value of the principle pslow is greater than the value of the Datum node, after the swap, or is greater than the Datum node

5) so traversing, knowing Pfast arrives Pend

6) At this point, in addition to the base node in the first position, followed by the less than the Datum node, larger than the two main nodes, you can pass Swap[pviot,pslow], so that the Datum node back to the middle position

listnode* getpartition (listnode* head,listnode* begin,listnode*End); ListNode* Qucik_sort (listnode*head,listnode* begin,listnode*End) {    if(Begin!=end) {//is the interval of [) and the last element corresponds to the end ()listnode* temp=getpartition (head,begin,end);        Qucik_sort (head,begin,temp); Qucik_sort (Head,temp-m_pnext,end); }    returnHead;} ListNode* Getpartition (listnode* head,listnode* begin,listnode*End) {    intPivot=begin->M_nvalue; ListNode*pslow=Begin; ListNode*pfast=pslow->M_pnext;  while(pfast!=End) {        if(pfast->m_nvalue<pivot) {Pslow=pslow->M_pnext; Std::swap (Pslow->m_nvalue,pfast->m_nvalue); } pfast=pfast->M_pnext; } std::swap (Pslow->m_nvalue,begin->m_nvalue); returnPslow;}

Qucik_sort (Head,head,null);

You may be surprised, how could it be a quick line? Fast row is not required a pointer to the head, a pointer to the tail, and then two pointers to the movement and a certain regularity to exchange values, finally find a fulcrum to the left side of the fulcrum is less than the fulcrum, the fulcrum right is greater than the fulcrum (this sentence is long, exhausted me)? It's a drop, the wood is wrong, but the problem comes out. If that's the case, we don't have a precursor pointer to a single-linked list, so how can we move the back pointer forward? So this kind of quick-line idea doesn't work, and if we can make two pointers move in the next direction and find the fulcrum, that's fine. How do you do it?

Next we use another way of thinking in the fast line to answer. We only need two pointers pslow and Pfast, both of which move in the next direction, while the moving process remains (Pivot,pslow] before the key is less than the selected key, (Pslow,pfast] The key is greater than the selected key, Then when Pfast to the end of the time to complete a fulcrum of the search. When the indicated element is more than the pivot hour, the slow moves forward one cell, exchanging the values of the slow and fast points, so that the element slow points to is less than the last element in the datum.

The core idea of the fast-line is to divide, to determine a pivot element (pivot), and each trip is divided into two parts, the first part is smaller than the pivot (sequence a), and the latter part is larger than the pivot (sequence B). After a trip, the sequence becomes: {A} pivot {B}. The following are the specific steps:
1. Determine the pivot element for each division is the head node of the current sequence to be ordered.
2. Set slow and fast two cursors, slow point to the last element in sequence a, and initialize to the pivot itself (the sequence head node to be ordered). Let fast traverse the sequence to be ordered, when the pointed element than the pivot hour, will slow forward a grid, exchange slow and fast the value of the element, so that the slow point to the element is still guaranteed to be the last element in sequence a.
3. Exchange slow the value of the element and pivot element.
4, repeat step 1~4 for sequence A and B.

End is the last identity bit of the region;
The slow should be for the large cell demarcation value, and belong to the community;
Fast should be renamed as a traversal cursor;
Listhead is the central value of the fast row comparison;
Finally swapping slow and listhead, identifying the first traversal compare sort end,
Starting with the remainder of the cell and the value of the large area recursively traverse the comparison sort;
Until the last two comparison sort the mobile node key value is the final sort, and the final stack output is the final sort result.

structNode {intkey; Node*Next; Node (intNkey, node*pnext): Key (Nkey), Next (Pnext) {}}; Node* Getpartion (node* pbegin, node*pEnd) {    intKey = Pbegin->key; Node* p =Pbegin; Node* Q = p->Next;  while(Q! =pEnd) {        if(Q->key <key) {P= p->Next; Swap (P->key,q->key); } q= q->Next; } Swap (P->key,pbegin->key); returnp;}voidQuickSort (node* pbeign, node*pEnd) {    if(Pbeign! =pEnd) {Node* Partion =getpartion (pbeign,pend);        QuickSort (pbeign,partion); QuickSort (partion-next,pend); }}

Quick sort of single-linked list

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.