Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:21:27+00:00 2026-06-05T14:21:27+00:00

I´m looking at example 6.7 from http://publications.gbdirect.co.uk/c_book/chapter6/structures.html (Pasted here for convenience) struct list_ele *

  • 0

I´m looking at example 6.7 from http://publications.gbdirect.co.uk/c_book/chapter6/structures.html

(Pasted here for convenience)

struct list_ele *
sortfun( struct list_ele *list )
{

      int exchange;
      struct list_ele *nextp, *thisp, dummy;

      /*
       * Algorithm is this:
       * Repeatedly scan list.
       * If two list items are out of order,
       * link them in the other way round.
       * Stop if a full pass is made and no
       * exchanges are required.
       * The whole business is confused by
       * working one element behind the
       * first one of interest.
       * This is because of the simple mechanics of
       * linking and unlinking elements.
       */

      dummy.pointer = list;
      do{
              exchange = 0;
              thisp = &dummy;
              while( (nextp = thisp->pointer)
                      && nextp->pointer){
                      if(nextp->data < nextp->pointer->data){
                              /* exchange */
                              exchange = 1;
                              thisp->pointer = nextp->pointer;
                              nextp->pointer =
                                      thisp->pointer->pointer;
                              thisp->pointer->pointer = nextp;
                      }
                      thisp = thisp->pointer;
              }
      }while(exchange);

      return(dummy.pointer);
}

I get the basic idea, but I cant really explain whats happening in there. Can someone explain in more depth but in simple manner whats going on in that sort function?

Some questions in general:

  • Why is dummy.pointer = list; needed? dummy is then returned at the end of the function, how come the list is still sorted?
  • What does the comment The whole business is confused by working one element behind the first one of interest. mean?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:21:29+00:00Added an answer on June 5, 2026 at 2:21 pm

    dummy is a local variable that is first set to the start of the list. The temporary variable thisp is set to point to dummy and so when it is updated, the contents pointed to by dummy are also updated. Thus, dummy.pointer will eventually point to the element that is the new beginning of the sorted list. list will still point the original start of the list, so the value is returned so the head pointer can be updated.

    I think what they mean by confusing is that element we are interested in is nextp, not the current element (or thisp). That is, we are comparing the next element in the list with the current element, rather than comparing the current one with the previous. I guess that’s confusing, but I don’t really find it so.

    Note: this is Bubble Sort. A better sort algorithm would be Merge Sort, with an implementation at http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
I am looking at an example found here: http://perlmeme.org/tutorials/sort_function.html And it gives this code
I'm looking at the TPL exception handling example from MSDN @ http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx The basic
I have been looking at a HTML 5 boilerplate template (from http://html5boilerplate.com/ ) and
I look an example of paginating from http://rapidprototype.ch/bg2docs/tg2pagination.html for my Turbogears 2 project and
I am looking at the example from http://www.javascriptkit.com/javatutors/oopjs.shtml var person = new Object() person.name
I just recently started looking into WatiN and was following the example from http://www.codeproject.com/KB/aspnet/WatiN.aspx
I've been looking into Java reflections. This is an example from Wikipedia http://en.wikipedia.org/wiki/Reflection_(computer_programming ):
thank you for looking i got this example from my book but i cant
I'm looking to remove duplicate phrases from any give string. Example: My_First_Post_My_First_Post.htm Would have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.