Help, bubble-sort algorithm

This is a discussion on Help, bubble-sort algorithm within the C Programming forums, part of the General Programming Boards category; Hi there im studying for exams going over previous exams and I have this question im not sure even where ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    Help, bubble-sort algorithm

    Hi there im studying for exams going over previous exams and I have this question im not sure even where to start ill show you the question and if anyone can help me that would be great.

    write a C functions strsort, using the bubble-sort algorithm, that has a single string as an argument.When the function finishes, the string is sorted in alphabetical order.

    e.g.

    after the following statements:
    char s[80] = "horse";
    strsort(s);

    s contains "ehors"

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Code:
    void bubbleSort (int list [],
                     int last)
    {
    
            int current;
    
             for(current = 0; current < last; current++)
                 bubbleUp (list, current, last);
    
            return;
    }       /* bubbleSort */
    
    void bubbleUp (int list[], 
                   int current,
                   int last)
    {
    
            int walker;
            int temp;
    
    
            for (walker = last; walker > current; walker--)
               if (list[walker]  < list[walker - 1])
                  {
                   temp             = list[walker];
                   list[walker]     = list[walker - 1];
                   list[walker - 1] = temp;
                  } /* if */
            return;
    }       /* bubbleUp */

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    thanks jennifer!!!

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    that bubble up function is damn confusing.. how exactly dose that work... what is the reason for having it?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    This is the output figure out how it work.

    Results:
    Unsorted array: 89 72 3 15 21 57 61 44 19 98 5 77 39 59 61
    Sorted array: 3 5 15 19 21 39 44 57 59 61 61 72 77 89 98

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    whicked! thanks alot!!! gonna check it out now!

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Remember jennifer, hints not answers right off the bat.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. testing a bubble sort algorithm
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 12:00 AM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. choosing right sort algorithm
    By Micko in forum C++ Programming
    Replies: 15
    Last Post: 05-29-2006, 10:38 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21