Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Insertion sort
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===List insertion sort code in C=== If the items are stored in a linked list, then the list can be sorted with O(1) additional space. The algorithm starts with an initially empty (and therefore trivially sorted) list. The input items are taken off the list one at a time, and then inserted in the proper place in the sorted list. When the input list is empty, the sorted list has the desired result. <syntaxhighlight lang="c" line="1"> struct LIST * SortList1(struct LIST * pList) { // zero or one element in list if (pList == NULL || pList->pNext == NULL) return pList; // head is the first element of resulting sorted list struct LIST * head = NULL; while (pList != NULL) { struct LIST * current = pList; pList = pList->pNext; if (head == NULL || current->iValue < head->iValue) { // insert into the head of the sorted list // or as the first element into an empty sorted list current->pNext = head; head = current; } else { // insert current element into proper position in non-empty sorted list struct LIST * p = head; while (p != NULL) { if (p->pNext == NULL || // last element of the sorted list current->iValue < p->pNext->iValue) // middle of the list { // insert into middle of the sorted list or as the last element current->pNext = p->pNext; p->pNext = current; break; // done } p = p->pNext; } } } return head; } </syntaxhighlight> The algorithm below uses a trailing pointer<ref>{{Citation |editor-last=Hill |editor-first=Curt |contribution=Trailing Pointer Technique |url=http://euler.vcsu.edu:7000/11421/ |title=Euler |publisher=Valley City State University |access-date=22 September 2012 |archive-date=26 April 2012 |archive-url=https://web.archive.org/web/20120426051041/http://euler.vcsu.edu:7000/11421/ |url-status=dead }}.</ref> for the insertion into the sorted list. A simpler recursive method rebuilds the list each time (rather than splicing) and can use O(''n'') stack space. <syntaxhighlight lang="c"> struct LIST { struct LIST * pNext; int iValue; }; struct LIST * SortList(struct LIST * pList) { // zero or one element in list if (!pList || !pList->pNext) return pList; /* build up the sorted array from the empty list */ struct LIST * pSorted = NULL; /* take items off the input list one by one until empty */ while (pList != NULL) { /* remember the head */ struct LIST * pHead = pList; /* trailing pointer for efficient splice */ struct LIST ** ppTrail = &pSorted; /* pop head off list */ pList = pList->pNext; /* splice head into sorted list at proper place */ while (!(*ppTrail == NULL || pHead->iValue < (*ppTrail)->iValue)) { /* does head belong here? */ /* no - continue down the list */ ppTrail = &(*ppTrail)->pNext; } pHead->pNext = *ppTrail; *ppTrail = pHead; } return pSorted; } </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Insertion sort
(section)
Add topic