Single-linked list merge sort

Source: Internet
Author: User

1, for the linked list, because the access of its elements can only be sequential access, and fast sorting is improved bubble sorting, need to randomly access data, inappropriate. The link list element is accessed by the merge sort method;

2. Required steps

1) Split: Divides a list into two parts

2) Sort: Merge the linked lists that are already ordered on both sides

3) Divide and conquer recursion: recursive operation


The linked list plist is split and the first element node of the segmented two-segment list is taken out through a two-level pointer. Where plist is the first element node pointer for the list
void Clinklist::splitlist (Clinknode * pList, Clinknode **pbegnode1, Clinknode **pbegnode2)
{
if (pList = = NULL)
{
Return
}

Clinknode *pfastnode = NULL;
Clinknode *pslownode = pList;

if (Pslownode = = NULL | | pslownode->pnext = = NULL)
{
*pbegnode1 = Pslownode;
*pbegnode2 = NULL;
Return

}


Pfastnode = pslownode->pnext;



while (Pfastnode! = NULL)
{

Pfastnode = pfastnode->pnext;
if (pfastnode! = NULL)
{
Pfastnode = pfastnode->pnext;
The slow pointer moves only when the fast pointer satisfies the condition of the backward move, or it causes the split to be uneven
Pslownode = pslownode->pnext;
}
printf ("pslownode[%d", pfastnode[%d]\n ", Pslownode->elem, Pfastnode->elem);

}

*pbegnode1 = pList;
*pbegnode2 = pslownode->pnext;

printf ("plist[%d", pslownode[%d]\n ", Plist->elem, Pslownode->elem);

Break a list from the middle

Pslownode->pnext = NULL;

}


Sort the two-part ordered list

Through a virtual node

Clinknode * Clinklist::sortlist (Clinknode * plista, Clinknode * plistb)
{

Clinknode Ctmpnode;
Ctmpnode.pnext = NULL;

Clinknode *ptmpnode = &CTmpNode;

if (Plista = = NULL)
{
return PLISTB;
}
else if (Plistb = = NULL)
{

return plista;
}


while (Plista! = NULL && PLISTB! = null)
{

if (Plista->elem <= Plistb->elem)
{
Ptmpnode->pnext= Plista;
Ptmpnode = ptmpnode->pnext;
Plista = plista->pnext;
}
Else
{
Ptmpnode->pnext= Plistb;
Ptmpnode = ptmpnode->pnext;
PLISTB = plistb->pnext;
}

}

if (Plista! = NULL)
{
Ptmpnode->pnext = Plista;
}
else if (plistb! = NULL)
{
Ptmpnode->pnext = PLISTB;
}



return ctmpnode.pnext;
}

Merge two linked lists recursively

Clinknode * CLINKLIST::SORTLIST1 (Clinknode * plista, Clinknode * plistb)
{
clinknode* PResult = NULL;

if (Plista = = NULL)
{
return PLISTB;
}
else if (Plistb = = NULL)
{
return plista;
}

//
if (Plista->elem <= Plistb->elem)
{
PResult = Plista;
Presult->pnext = SortList1 (Plista->pnext, PLISTB);
}
Else
{
PResult = PLISTB;
Presult->pnext = SortList1 (Plista, Plistb->pnext);

}
return pResult;

}

Divide and conquer recursion

parameter is out parameter

void Clinklist::mergeslist (Clinknode * * plistnode)
{

if (*plistnode = = NULL | | (*plistnode)->pnext = = NULL)
{
Return
}

Clinknode *pfrontnode = NULL;
Clinknode *pbacknode = NULL;
Linked list segmentation because it cannot be accessed randomly
Splitlist (*plistnode, &pfrontnode, &pbacknode);


Recursive call, recursive to exit without node
Mergeslist (&pfrontnode);
Mergeslist (&pbacknode);

Two list of lists sorted
*plistnode = SortList1 (Pfrontnode, Pbacknode);

}

Single-linked list merge sort

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.