Step-by-Step write algorithm (list sort)

Source: Internet
Author: User

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)

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.