One-step write algorithm (sort by linked list)

Source: Internet
Author: User

 

[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;

}

Related Article

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.