Sort linked lists (bubble and merge)

Source: Internet
Author: User
Tags return tag
There are many sorting algorithms. The following describes how to implement the bubble sort and merge sort of connectors. The Ascending Order is used as an example. Principle of Bubble Sorting: each trip compares the data parts of two adjacent connectors. If the refer component is in the front, the two connectors are switched to form a successor, until the values of all data parts are listed in ascending order. Principle of merging and sorting: recursively divides the connected list until the smallest division, and then merges the connected list in ascending order until all the divisions are merged. The comparison of data parts is determined by calling the return value of the callback function. The callback function has a specific application implementation.


Typedef int (* linksortcall) (linkptr plk1, linkptr plk2, void * PARAM );
/**//*
Function: callback function for defining the sort of connectors
Parameter: plk1, plk2 is the data connector pointer, And Param is the return parameter.
Return Value: 0 is (plk1) = (plk2), 1 is (plk1)> (plk2), and-1 is (plk1) <(plk2)
*/
 
Xdl_api void bubblesortlink (linkptr root, linksortcall PF, void * parm );
/**//*
Function: Performs Bubble Sorting on the linked list.
Parameter: Root is the root connector pointer, PF is the sort callback function, And Param is the callback parameter.
Return: None
*/
 
Xdl_api void mergesortlink (linkptr root, linksortcall PF, void * parm );
/**//*
Function: combines and sorts linked lists.
Parameter: Root is the root connector pointer, PF is the sort callback function, And Param is the callback parameter.
Return: None
*/
 
Void bubblesortlink (linkptr root, linksortcall PF, void * parm)
...{
Linkptr Prev, next;
Int tag = 1;
 
Assert (root & root-> tag = lkroot );
Assert (PF );
 
While (TAG)
...{
Tag = 0;
Prev = getfirstlink (Root );
While (prev)
...{
Next = getnextlink (prev );
If (next = NULL)
Break;
 
If (* PF) (prev, next, parm)> 0)
...{
Switchlinktonext (prev );
Tag = 1;
} Else
Prev = next;
}
}
}
 
// Division of connectors
Int _ divlink (linkptr root1, linkptr root2)
...{
Int count, tag;
 
Tag = linkcount (root1 );
Count = tag/2;
While (count --)
...{
Insertlink (root2, link_first, deletelink (root1, link_last ));
}
Return tag;
}
 
// Merged Connector
Void _ mrglink (linkptr root1, linkptr root2, linksortcall PF, void * parm)
...{
Linkptr next1, next2;
Linkptr PLK;
 
Next1 = getfirstlink (root1 );
Next2 = getfirstlink (root2 );

While (next1! = NULL & next2! = NULL)
...{
If (* PF) (next1, next2, parm)> 0)
...{
PLK = getnextlink (next2 );
Insertlinkbefore (root1, next1, deletelink (root2, next2 ));
Next2 = PLK;
} Else
...{
Next1 = getnextlink (next1 );
}
}
 
While (next2! = NULL)
...{
PLK = getnextlink (next2 );
Insertlink (root1, link_last, deletelink (root2, next2 ));
Next2 = PLK;
}

}
 
Void mergesortlink (linkptr root, linksortcall PF, void * parm)
...{
Link LK;

Initrootlink (& lk );
 
If (_ divlink (root, & lk)> 1)
...{
Mergesortlink (root, PF, parm );
Mergesortlink (& LK, PF, parm );
_ Mrglink (root, & LK, PF, parm );
}
}

Collected by barenx

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.