Thread: Bubble Sort Query

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6

    Bubble Sort Query

    Hi All,

    in the below code, the author uses what he labels as a code to re-order the elements of a bubble. Some of my colleagues believe that it is not truly a bubble process. Is this true? They couldn't recall what it is really called, what the author is doing. Anyone know or disagree? Cheers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ARSIZE  10
    int main(){
            int ch_arr[ARSIZE],count1;
            int count2, stop, lastchar;
    
            lastchar = 0;
            stop = 0;
            /*
     *          * Read characters into array.
     *                   * Stop if end of line, or array full.
     *                            */
            while(stop != 1){
                    ch_arr[lastchar] = getchar();
                    if(ch_arr[lastchar] == '\n')
                            stop = 1;
                    else
                            lastchar = lastchar + 1;
                    if(lastchar == ARSIZE)
                            stop = 1;
            }
            lastchar = lastchar-1;
    
            /*
     *          * Now the traditional bubble sort.
     *                   */
            count1 = 0;
            while(count1 < lastchar){
                    count2 = count1 + 1;
                    while(count2 <= lastchar){
                            if(ch_arr[count1] > ch_arr[count2]){
                                    /* swap */
                                    int temp;
                                    temp = ch_arr[count1];
                                    ch_arr[count1] = ch_arr[count2];
                                    ch_arr[count2] = temp;
                            }
                            count2 = count2 + 1;
                    }
                    count1 = count1 + 1;
            }
    
            count1 = 0;
            while(count1 <= lastchar){
                    printf("%c\n", ch_arr[count1]);
                    count1 = count1 + 1;
            }
            exit(EXIT_SUCCESS);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it is bubble sort, just instead of for loops - it uses while loops (I see no real reason why)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by vart View Post
    it is bubble sort, just instead of for loops - it uses while loops (I see no real reason why)
    Actually, no it's not.
    If it doesn't always compare adjacent items then it is not Bubble Sort. Since count2 can be more than one greater than count1 in this implementation, it is not Bubble Sort. Because Bubble Sort always compares adjacent items, it is stable. This implementation is not stable.
    Some people call this "Simple Sort".

    These algorithms both appear in my sorting algorithm visualiser that you can download from my site (see my sig). What you have here does not exhibit the tell-tale "bubble" effect during sorting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  3. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  4. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM