# 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.

/**//*
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)
*/

/**//*
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
*/

/**//*
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
*/

...{
Int tag = 1;

Assert (root & root-> tag = lkroot );
Assert (PF );

While (TAG)
...{
Tag = 0;
While (prev)
...{
If (next = NULL)
Break;

If (* PF) (prev, next, parm)> 0)
...{
Tag = 1;
} Else
Prev = next;
}
}
}

// Division of connectors
...{
Int count, tag;

Count = tag/2;
While (count --)
...{
}
Return tag;
}

// Merged Connector
...{

While (next1! = NULL & next2! = NULL)
...{
If (* PF) (next1, next2, parm)> 0)
...{
Next2 = PLK;
} Else
...{
}
}

While (next2! = NULL)
...{
Next2 = PLK;
}

}

...{

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

Collected by barenx

Related Keywords:

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.