Help understanding bubblesort hw?

This is a discussion on Help understanding bubblesort hw? within the C Programming forums, part of the General Programming Boards category; *slaps head* i look in wrong area, i understand what you are saying ok now how do i call bubblesort ...

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    *slaps head* i look in wrong area, i understand what you are saying

    ok now how do i call bubblesort into my actual program?
    Last edited by beretta; 04-20-2009 at 12:37 AM.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No. Code is executed line by line, in order. In other words if I have:
    Code:
    int i;
    if(i == 5)
        printf("i is equal to 5\n");
    i = 5;
    In the above code, the check if i is equal to five happens before i is set to 5.

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    yea i understand, i apologize about that.

    so if i added cmp = 1;

    does the sort look valid?

    if so how do i actually use it in the program

  4. #19
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Your code is still wrong. The bubblesort() function will never return; it will loop forever. Look at your loop conditions. The outer loop will run forever because cmp will always be less than or equal to n.

    As for how to use your program, here is one method:
    Code:
    int main(void)
    {
        int a[3] = { 3, 2, 1 };
        bubblesort(a, sizeof(a) / sizeof(int));
        return 0;
    }

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    hmm, i'm a bit lost honestly.

  6. #21
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Here is your code with a few changes to the main function so it will run:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void bubblesort(int a[],int n)
    {
        int cmp = 1, pass, tmp;
        pass = 1;
        while(cmp <= n)
        {
            printf("Inside outer loop.  cmp=%d, n=%d\n",cmp,n);
            cmp = 1;
            while(cmp <= n - pass)
            {
                printf("Inside inner loop.  cmp=%d, n-pass=%d\n",cmp,n-pass);
                if(a[cmp-1] > a[cmp])
                {
                    printf("Swapping variables\n");
                    tmp = a[cmp-1];
                    a[cmp-1] = a[cmp];
                    a[cmp] = tmp;
                }
                cmp = cmp + 1;
            }
            pass = pass + 1;
        }
    }
    
    int main(void)
    {
        int a[3] = { 3, 2, 1 };
        bubblesort(a, sizeof(a) / sizeof(int));
        return 0;
    }
    Now compile and run that code. What happens?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I liked where you where with this code, Baretta. I've taken the liberty of just re-arranging it so it's "right side up" (has the includes at the top of the program).


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void bubblesort(int a[],int n); 
    
    int main()
    {
        int a[10] = { 3, 1, 7, 6, 2, 8, 9, 5, 4, 0 };
        int cmp, pass, tmp, n; //we can remove this line of code, since it belongs in the bubblesort() function.
                     //printf("Please enter an integer: ");
                     //scanf("%d", &n);
        
        
        system("pause");
        return 0;  
    }
    
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
       int cmp, pass, tmp;
          
    
       pass = cmp = 0;      <-- We haven't made any passes or comparisons, yet
          while(cmp < n)     <-- Just less than n, not equal to, this is C 
          {
            cmp = 1;            <- instead of =1, let's increment it by one: cmp++;
            while(cmp <= n - pass)  <-- looks correct
             {
               if(a[cmp-1] > a[cmp])
              {
                     tmp = a[cmp-1]; 
                     a[cmp-1] = a[cmp]; 
                     a[cmp] = tmp;
              }
              cmp = cmp + 1;
          }
          pass = pass + 1;
       }
    }    
    
    /* The actual method of sorting */
    Last edited by Adak; 04-20-2009 at 01:19 AM.

  8. #23
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The actual method of sorting */
    
    void bubblesort(int a[],int n);
    
    int main()
    {
        int a[4];
        int n;
        printf("Please enter an integer: ");
        scanf("%d", &n);
        bubblesort(a, sizeof(a) / sizeof(int));
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
          int cmp, pass, tmp;
          pass = 1;
          cmp = 1;
          while(cmp <= n)
          {
            cmp = 1;
            while(cmp <= n - pass)
             {
               if(a[cmp-1] > a[cmp])
                             {
                                tmp = a[cmp-1]; 
                                a[cmp-1] = a[cmp]; 
                                a[cmp] = tmp;
                             }
               cmp = cmp + 1;
             }
            pass = pass + 1;
          }
    }
    thank you for the arrangement help
    does that look any better?

    it compiles and i can enter n, but it does nothing afterwards

  9. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    hmm i see i forgot the add an output that would state the array. hmm

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's hard to believe, but when you are designing a program, in the first half of the design process, ignore the damn details.

    Whether the program works is at this time, a mere detail. Welcome to top-down design!

    Did you make the other changes in the code as I marked them out, previously?

    *bithub: It's after Midnight, and you're asking a young person to waste sleep to prove that his code will loop endlessly, as you stated.

    That seems unnecessary at the very least, at this hour.
    Last edited by Adak; 04-20-2009 at 01:26 AM.

  11. #26
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    does that look any better?
    It looks better, but your indentation still needs some help. Look at the code I posted; it is properly indented.

  12. #27
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The actual method of sorting */
    
    void bubblesort(int a[],int n);
    
    int main()
    {
        int a[4]; 
        int n;
        printf("Please enter an integer: ");
        scanf("%d", &n);
        bubblesort(a, sizeof(a) / sizeof(int));
        
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
        int cmp, pass, tmp;
        pass = 1;
        cmp = 1;
        printf("Inside outer loop.  cmp=%d, n=%d\n",cmp,n);
        while(cmp <= n - pass)
        {
            printf("Inside inner loop.  cmp=%d, n-pass=%d\n",cmp,n-pass);
            if(a[cmp-1] > a[cmp])
            {
                tmp = a[cmp-1]; 
                a[cmp-1] = a[cmp]; 
                a[cmp] = tmp;
            }
            cmp = cmp + 1;
        }
        pass = pass + 1;  
    }
    it's working and I fixed the indentions for you sir =)
    Last edited by beretta; 04-20-2009 at 01:27 AM.

  13. #28
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    it's working!
    No it's not. Your code isn't sorting anything. Bubble sorts require 2 loops, you just have one now...

  14. #29
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    well i meant that some code is actually showing. lol.

    hmmmmmm

  15. #30
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You still have a problem in your main function. You are not assigning any values to the array you are sorting. Put some values in there!
    Code:
    int main(void)
    {
        int i;
        int a[4] = {4, 8, 2, 1}; 
        printf("Before calling bubblesort()...\n");
        for(i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
            printf("%d ", a[i]);
        printf("\n");
        bubblesort(a, sizeof(a) / sizeof(int));
        printf("After calling bubblesort()...\n");
        for(i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
            printf("%d ", a[i]);
        printf("\n");
        system("pause");
        return 0;  
    }

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Replies: 10
    Last Post: 12-05-2008, 11:47 AM
  3. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  4. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM

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