<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: link-list sorting in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082952#M92744</link>
    <description>... and most implementations of linked lists that do need to be ordered insert the elements in the correct location so that no sorting is ever required. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Wed, 05 Dec 2007 20:41:29 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2007-12-05T20:41:29Z</dc:date>
    <item>
      <title>link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082948#M92740</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Pasted below is my procedure(SortList) to sort the singly link-list,but this is not working.&lt;BR /&gt;i might be doing something stupid,and i would really appreciate if you guys&lt;BR /&gt;can let me realize the mistake in my code(a silly code with no importance)&lt;BR /&gt;&lt;BR /&gt;4 struct node{&lt;BR /&gt;5 int data;&lt;BR /&gt;6 struct node *next;&lt;BR /&gt;7 };&lt;BR /&gt;&lt;BR /&gt;29 /* returns the length of the link list*/&lt;BR /&gt;30 int Length(struct node* head){&lt;BR /&gt;31 struct node *current = head;&lt;BR /&gt;32 int count = 0;&lt;BR /&gt;33 while(current != NULL){&lt;BR /&gt;34 count++;&lt;BR /&gt;35 current = current-&amp;gt;next;&lt;BR /&gt;36 }&lt;BR /&gt;37 return(count);&lt;BR /&gt;38 }&lt;BR /&gt;&lt;BR /&gt;40 /* prints the data field of the each node*/&lt;BR /&gt;41 void LinkTraverse(struct node* head){&lt;BR /&gt;42 struct node* current = head;&lt;BR /&gt;43 int i = 1;&lt;BR /&gt;44 while(current != NULL){&lt;BR /&gt;45 printf("element %d is %d\n",i,current-&amp;gt;data);&lt;BR /&gt;46 i++;&lt;BR /&gt;47 current = current-&amp;gt;next;&lt;BR /&gt;48 }&lt;BR /&gt;49 }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;140 /*sort the link*/&lt;BR /&gt;141 void SortList(struct node** headRef){&lt;BR /&gt;142 struct node* current = *headRef;&lt;BR /&gt;143 int i,j,temp,length;&lt;BR /&gt;144 length = Length(current);&lt;BR /&gt;145 for(i = length;i &amp;gt;= 1 ; i--){&lt;BR /&gt;146 for(j = 1; j &amp;lt;= i; j++){&lt;BR /&gt;147 if(current-&amp;gt;data &amp;gt; current-&amp;gt;next-&amp;gt;data){&lt;BR /&gt;148 temp = current-&amp;gt;data;&lt;BR /&gt;149 current-&amp;gt;data = current-&amp;gt;next-&amp;gt;data;&lt;BR /&gt;150 current-&amp;gt;next-&amp;gt;data = temp;&lt;BR /&gt;151 }&lt;BR /&gt;152 }&lt;BR /&gt;153 }&lt;BR /&gt;154 LinkTraverse(head);&lt;BR /&gt;155 }&lt;BR /&gt;&lt;BR /&gt;~amit</description>
      <pubDate>Wed, 05 Dec 2007 13:48:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082948#M92740</guid>
      <dc:creator>amit mehta_2</dc:creator>
      <dc:date>2007-12-05T13:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082949#M92741</link>
      <description>please read LinkTraverse(head);&lt;BR /&gt;as LinkTraverse(current);&lt;BR /&gt;&lt;BR /&gt;~amit</description>
      <pubDate>Wed, 05 Dec 2007 13:50:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082949#M92741</guid>
      <dc:creator>amit mehta_2</dc:creator>
      <dc:date>2007-12-05T13:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082950#M92742</link>
      <description>Hi Amit,&lt;BR /&gt;&lt;BR /&gt;you never move your current pointer through the list and always compare the same current with current-&amp;gt;next;&lt;BR /&gt;&lt;BR /&gt;you miss something like &lt;BR /&gt;&lt;BR /&gt;145.5 current=*headRef;       &lt;BR /&gt;/* begin the walk trough the list each for-i-iteration */&lt;BR /&gt;&lt;BR /&gt;and &lt;BR /&gt;&lt;BR /&gt;150.5 current=current-&amp;gt;next;  &lt;BR /&gt;/* step one listelement foreward each for-j iteration */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Hint: dont exchange data but pointers, imagine your data is not only "int" but something multi megabyte sized. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;rgds&lt;BR /&gt;HGH&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Dec 2007 18:03:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082950#M92742</guid>
      <dc:creator>Hemmetter</dc:creator>
      <dc:date>2007-12-05T18:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082951#M92743</link>
      <description>Few questions regarding your post. Why do you want to singly-link integers when they can all be put into a big array? Unless you are allocating memory for each of them dynamically but for that I see no calls to malloc(3C). Also have you looked at either quick sort or binary sort. Those algorithms have been tried and tested for integer data. Might be better to run your data through them before developing a new kind of sort.</description>
      <pubDate>Wed, 05 Dec 2007 18:25:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082951#M92743</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-12-05T18:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082952#M92744</link>
      <description>... and most implementations of linked lists that do need to be ordered insert the elements in the correct location so that no sorting is ever required. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Dec 2007 20:41:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082952#M92744</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-12-05T20:41:29Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082953#M92745</link>
      <description>&lt;!--!*#--&gt;You may want to use a real language like C++ to do all of this:&lt;BR /&gt;&lt;BR /&gt;std::list&lt;INT&gt; i_list;  // doubly linked list&lt;BR /&gt;i.size()&lt;BR /&gt;&lt;BR /&gt;std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream o, std::list&lt;INT&gt; l) {&lt;BR /&gt;   for (std::list&lt;INT&gt;::iterator i = l.begin(); i != l.end(); ++i)&lt;BR /&gt;      std::cout &amp;lt;&amp;lt; *i;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;i.sort();&lt;BR /&gt;&lt;BR /&gt;&lt;/INT&gt;&lt;/INT&gt;&lt;/INT&gt;</description>
      <pubDate>Thu, 06 Dec 2007 01:26:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082953#M92745</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-06T01:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082954#M92746</link>
      <description>Thanks to all of you for these suggestions.&lt;BR /&gt;&lt;BR /&gt;Sandman, pasted below is a small code for depiciting bubble sort:&lt;BR /&gt;&lt;BR /&gt;      1 #include &lt;STDIO.H&gt;&lt;BR /&gt;      2 #include &lt;STDLIB.H&gt;&lt;BR /&gt;      3&lt;BR /&gt;      4 void BubbleSort(int numbers[],int arraySize){&lt;BR /&gt;      5    int i,j,temp;&lt;BR /&gt;      6    for(i = (arraySize - 1) ; i &amp;gt;= 0 ; i--){&lt;BR /&gt;      7       for(j = 1; j &amp;lt;= i; j++){&lt;BR /&gt;      8          if(numbers[j - 1] &amp;gt; numbers[j]){&lt;BR /&gt;      9             temp = numbers[j - 1];&lt;BR /&gt;     10             numbers[j - 1] = numbers[j];&lt;BR /&gt;     11             numbers[j] = temp;&lt;BR /&gt;     12          }&lt;BR /&gt;     13       }&lt;BR /&gt;     14    }&lt;BR /&gt;     15    i = 0;&lt;BR /&gt;     16    while(i != arraySize){&lt;BR /&gt;     17       printf("%d\n",numbers[i]);&lt;BR /&gt;     18       i++;&lt;BR /&gt;     19    }&lt;BR /&gt;     20 }&lt;BR /&gt;     21 int main(void){&lt;BR /&gt;     22    int numbers[5] = {2,12,4,3,9};&lt;BR /&gt;     23    BubbleSort(numbers,5);&lt;BR /&gt;     24    exit(EXIT_SUCCESS);&lt;BR /&gt;     25 }&lt;BR /&gt;&lt;BR /&gt;Also in my previous post i missed to post the  list building function,in which i have used malloc for getting memory from the heap.&lt;BR /&gt;&lt;BR /&gt;     10 struct node* BuildOneTwoThree(void){&lt;BR /&gt;     11    struct node *head,*second,*third;&lt;BR /&gt;     12    third = second = head = NULL;&lt;BR /&gt;     13    head = (struct node*)malloc(sizeof(struct node));&lt;BR /&gt;     14    second = (struct node*)malloc(sizeof(struct node));&lt;BR /&gt;     15    third = (struct node*)malloc(sizeof(struct node));&lt;BR /&gt;     16&lt;BR /&gt;     17    head-&amp;gt;data = 12;&lt;BR /&gt;     18    head-&amp;gt;next = second;&lt;BR /&gt;     19&lt;BR /&gt;     20    second-&amp;gt;data = 2;&lt;BR /&gt;     21    second-&amp;gt;next = third;&lt;BR /&gt;     22&lt;BR /&gt;     23    third-&amp;gt;data = 7;&lt;BR /&gt;     24    third-&amp;gt;next = NULL;&lt;BR /&gt;     25&lt;BR /&gt;     26    return(head);&lt;BR /&gt;     27 }&lt;BR /&gt;&lt;BR /&gt;HGH,&lt;BR /&gt;thanks a lot for pointing the mistake(current pointer left unincremented), after doing the same,still there was no change in the output :(&lt;BR /&gt;&lt;BR /&gt;~amit&lt;/STDLIB.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 06 Dec 2007 06:31:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082954#M92746</guid>
      <dc:creator>amit mehta_2</dc:creator>
      <dc:date>2007-12-06T06:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082955#M92747</link>
      <description>&lt;!--!*#--&gt;Change your loops to:&lt;BR /&gt;for (i = length; i &amp;gt; 1; --i) {&lt;BR /&gt;   current = *headRef;&lt;BR /&gt;   for (j = 1; j &amp;lt; i; ++j){&lt;BR /&gt;      register struct node *n = current-&amp;gt;next;&lt;BR /&gt;      if (current-&amp;gt;data &amp;gt; n-&amp;gt;data){&lt;BR /&gt;         temp = current-&amp;gt;data;&lt;BR /&gt;         current-&amp;gt;data = n-&amp;gt;data;&lt;BR /&gt;         n-&amp;gt;data = temp;&lt;BR /&gt;      }&lt;BR /&gt;      current = n;&lt;BR /&gt;   }&lt;BR /&gt;}&lt;BR /&gt;LinkTraverse(*headRef);&lt;BR /&gt;&lt;BR /&gt;Note SortList should really be declared as:&lt;BR /&gt;void SortList(struct node *head);&lt;BR /&gt;&lt;BR /&gt;Unless you are going to do the pointer interchange as HGH hinted.</description>
      <pubDate>Thu, 06 Dec 2007 07:05:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082955#M92747</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-06T07:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082956#M92748</link>
      <description>&amp;gt;pasted below is a small code for depicting bubble sort:&lt;BR /&gt;&lt;BR /&gt;I forgot to mention, the only use of a bubble sort is to put on a wanted poster of a poster child of what not to do.  :-)&lt;BR /&gt;A shell sort is much faster and about as easy to program.  Especially if you have random access iterators as in your array.</description>
      <pubDate>Thu, 06 Dec 2007 07:08:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082956#M92748</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-06T07:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082957#M92749</link>
      <description>Thanks Dennis,and also to everyone else.&lt;BR /&gt;Points to follow :)&lt;BR /&gt;</description>
      <pubDate>Thu, 06 Dec 2007 07:32:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082957#M92749</guid>
      <dc:creator>amit mehta_2</dc:creator>
      <dc:date>2007-12-06T07:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: link-list sorting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082958#M92750</link>
      <description>Closing the thread.</description>
      <pubDate>Thu, 06 Dec 2007 09:19:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/link-list-sorting/m-p/5082958#M92750</guid>
      <dc:creator>amit mehta_2</dc:creator>
      <dc:date>2007-12-06T09:19:45Z</dc:date>
    </item>
  </channel>
</rss>

