Text: Step by step Write algorithm (list sort)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Comparing the ordering of linear tables, the contents of the list sort are a little more troublesome. On the one hand, you have to consider the steps of data insertion, and on the other hand you have to be wary of pointers. If one step is wrong, then the operating system will give you a exception immediately. In terms of the particularity of the list, what are the sorts that are suitable for the list?
(1) Insert sort (FIT)
(2) bubble sort (suitable)
(3) Hill sort (suitable)
(4) Select sort (FIT)
(5) Quick sort (not suitable)
(6) Merge sort (not suitable)
(7) Base order (not suitable)
(8) heap sorting (not suitable)
In fact, generally speaking. When it comes to relative leveling between data, it is only suitable for linear sorting, and if it is only the exchange between data content, then this sort method is more suitable for the sorting of the linked list. Fast sorting, merging sorting, and heap sorting all involve the selection of intermediate values, so it is not suitable for list sorting.
To illustrate how the list sort is done, we can use the insert sort as an example to describe how the list of links is inserted and sorted.
a) first traverse the node, one side is the sorted node, the other 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;}
b) for the node to be inserted, select the appropriate location to insert
void Insert_for_sort_operation (node** ppnode, node* pnode) {node* prev; node* cur;/* Insert Pnode */if before the first data (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;}
Step-by-Step write algorithm (list sort)