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; Code: #include <stdio.h> #include <stdlib.h> /* The actual method of sorting */ void bubblesort(int a[],int n); int main() { int ...

  1. #31
    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[10]; 
        bubblesort(a, sizeof(a) / sizeof(int));
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
        printf("Please enter an integer: ");
        scanf("%d", &n);
        int cmp, pass, tmp;
        pass = 1;
        cmp = 1;
        while(cmp <= n)
        {
            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; 
        }     
    }
    that seems to work better

  2. #32
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    bithub, we do not use the for function in our class

  3. #33
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,254
    That code is just to help you see what is going on with your program. Anyways, the same thing can be implemented with a while loop.
    Code:
    int main(void)
    {
        int i = 0;
        int a[4] = {4, 8, 2, 1}; 
        printf("Before calling bubblesort()...\n");
        while(i < sizeof(a) / sizeof(a[0])
            printf("%d ", a[i++]);
        printf("\n");
        bubblesort(a, sizeof(a) / sizeof(int));
        ...
    }

  4. #34
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    just mentioning it to you sir, I understand what you are saying though. thank you.

  5. #35
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    hmm im not understanding how to view after.

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Only while loops, eh?

    How about something like

    Code:
    while( i < n)
      print a[i]
    This is NOT code, just pseudo-code.

  7. #37
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    i got that, now i need to run a loop to where it finishes the sort

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, I had to make some changes to get this version to sort. I'm not sure it's everything your teacher wants, however.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void bubblesort(int a[],int n); 
    void showIt(int a[], int n);
    
    int main()
    {
        int a[10] = { 3, 1, 7, 6, 2, 8, 9, 5, 4, 0 };
        int n = 10;
    
        bubblesort(a, n);
        showIt(a, n);
        printf("\n\n\t\t\t     press enter when ready");
        n = getchar();
        return 0;  
    }
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
       int cmp, pass, tmp, swapped;
    
       pass = cmp = 0;  
       swapped = 1;
       while(swapped)
       {
          swapped = 0;
          cmp = 1;          
          while(cmp < n - pass)  
          {
             if(a[cmp-1] > a[cmp])
             {
                swapped = 1;
                tmp = a[cmp-1]; 
                a[cmp-1] = a[cmp]; 
                a[cmp] = tmp;
             }
             cmp++;
          }
          pass++;
       }
       printf("\n Our Sort had %d comparisons, and %d passes", cmp, pass);
    }    
    void showIt(int a[], int n)  {
      int i;
      printf("\n\n\n");
    
      for(i = 0; i < n; i++)
         printf("%d ", a[i]);
    }
    I don't like the cmp being reset to 1, but it appears part and parcel of this version of the bubblesort.

  9. #39
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    hmmm

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The actual method of sorting */
    
    void bubblesort(int a[],int n);
    
    int main()
    {
        int list = 0;
        int a[4] = { 4, 8, 2, 1};
        printf("Before: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        bubblesort(a, sizeof(a) / sizeof(int));
        list = 0;
        printf("After: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
        printf("Please enter an integer: ");
        scanf("%d", &n);
        int cmp, pass, tmp;
        pass = 1;
        cmp = 1;
        while(cmp <= n)
        {
            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++;
            pass++; 
        }     
    }
    this is what i have so far.

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The output is in sorted order, and all array values are present.

    I am receiving no warning or errors, of any kind, and have run this numerous times.

    What's not working?

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As bithub noted, you must have two nested loops to run a bubblesort. (actually, any sort I ever heard of).

    You only have 1.

    You can either keep the one loop, and have it not sort, or add another loop to it, and have it sort.

  12. #42
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    so your showit is your second loop?

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, showIt just prints out the array, it sorts nothing.

    Look in bubblesort(), and see the two while loops, nested inside each other?

    That's what's doing the sorting.

  14. #44
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    how would i loop with

    Code:
     printf("Please enter an integer: ");
        scanf("%d", &n);
        int cmp, pass, tmp, swch;
        pass = 1;
        cmp = 1;
        swch = 0;
        while(swch)
        {
            swch = 1;
            cmp = 0;
            while(cmp < n - pass)
            {
                if(a[cmp-1] > a[cmp])
                {
                     swch = 0;
                     tmp = a[cmp-1]; 
                     a[cmp-1] = a[cmp]; 
                     a[cmp] = tmp;
                }
                cmp++;
            }    
            pass++; 
        }
    cause that does nothing.

  15. #45
    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 list = 0;
        int a[4] = { 4, 8, 2, 1};
        printf("Before: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        bubblesort(a, sizeof(a) / sizeof(int));
        list = 0;
        printf("After: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
        printf("Please enter an integer: ");
        scanf("%d", &n);
        int cmp, pass, tmp, swch;
        pass = 0;
        cmp = 0;
        swch = 1;
        while(swch)
        {
            swch = 0;
            cmp = 1;
            while(cmp < n - pass)
            {
                if(a[cmp-1] > a[cmp])
                {
                     swch = 1;
                     tmp = a[cmp-1]; 
                     a[cmp-1] = a[cmp]; 
                     a[cmp] = tmp;
                }
                cmp++;
            }    
            pass++; 
        }     
        printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass);
    }
    this works. now i need it to stop when it is finished sorting.

Page 3 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, 05:21 PM
  2. Replies: 10
    Last Post: 12-05-2008, 12:47 PM
  3. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 06:53 AM
  4. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 07:46 PM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM

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