[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Compared with linear table sorting, the content of chain table sorting is a little more troublesome. On the one hand, you need to consider the data insertion steps; on the other hand, you also need to worry about pointers. If one step of the content is wrong, the operating system will immediately bring you an exception. In terms of the particularity of the linked list, what are the sorting methods suitable for the linked list?
(1) Insert sorting (applicable)
(2) Bubble Sorting (suitable)
(3) Hill sorting (applicable)
(4) Select sorting (applicable)
(5) Fast sorting (not suitable)
(6) Merge and sort (not suitable)
(7) Base sorting (not suitable)
(8) Heap sorting (not suitable)
In fact, in general. If the allocation of the relative relationship between data is involved, it is only suitable for Linear sorting. If only the exchange of data content, then this sorting method is also suitable for the sorting of linked lists. Fast sorting, Merge Sorting, and heap sorting all involve the selection of intermediate values, so it is not suitable for sorting linked lists.
To illustrate how the chain table is sorted, we can use insertion sorting as an example to describe how the chain table is inserted and sorted.
A) First, traverse the node. One side is the sorted node and the other side is the node to be sorted.
Void sort_for_link_node (NODE ** ppNode)
{
NODE * prev;
NODE * curr;
If (NULL = ppNode | NULL = * ppNode)
Return;
Curr = (* ppNode)-> next;
(* PpNode)-> next = NULL;
While (curr ){
Prev = curr;
Curr = curr-> next;
Insert_for_sort_operation (ppNode, prev );
}
Return;
}
Void sort_for_link_node (NODE ** ppNode)
{
NODE * prev;
NODE * curr;
If (NULL = ppNode | NULL = * ppNode)
Return;
Curr = (* ppNode)-> next;
(* PpNode)-> next = NULL;
While (curr ){
Prev = curr;
Curr = curr-> next;
Insert_for_sort_operation (ppNode, prev );
}
Return;
}
B) Select a proper position for the node to be inserted.
Void insert_for_sort_operation (NODE ** ppNode, NODE * pNode)
{
NODE * prev;
NODE * cur;
/* Insert pNode before the first data */
If (pNode-> data <(* ppNode)-> data ){
PNode-> next = * ppNode;
* PpNode = pNode;
Return;
}
Cur = * ppNode;
While (cur ){
If (pNode-> data <cur-> data)
Break;
Prev = cur;
Cur = cur-> next;
}
PNode-> next = prev-> next;
Prev-> next = pNode;
Return;
}