Thread: Help me understand this

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    6

    Post Help me understand this

    Okay, I'm studying about the bubble sorting thing. I am able to do it, but the post that guide me, didn't explain one part.

    Can someone explain me the indicated part like explaining it to a beginner?
    Because, I can't really get what is really happening in this part of the source code.

    Please, explain the indicated part, thanks!

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int a[5], b, c, d;
    clrscr();
    printf("ENTER 5 NO \n\n");
    for(b=0;b<5;b++)
    scanf("%d", &a[b]);
    
    /*This part, help me with this*/
    for(b=0;b<5;b++)
    {
    for(c=b+1;c<5++)
    {
    if(a>a[c])
    {
    d=a;
    a=a[c];
    a[c]=d;
    }
    }
    }
    /*Until here*/
    
    for(b=0;b<5;b++)
    {
    printf("\n%d\n", a[b]);
    }
    getche();
    }
    Last edited by forpetesake568; 10-27-2015 at 09:26 PM.

  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    For a start, look at the third for-loop for(c=b+1;c<5++) does this look right..?

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    6
    Nah, its a typo. didn't noticed that earlier. It should be "(c=b+1;c<5;c++)" for the third for-loop. Sorry about that.
    Last edited by forpetesake568; 10-27-2015 at 10:54 PM.

  4. #4
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi..

    Does the code compile...without errors

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first step is to indent the code properly.
    https://en.wikipedia.org/wiki/Indent_style

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
      int a[5], b, c, d;
      clrscr();
      printf("ENTER 5 NO \n\n");
      for (b = 0; b < 5; b++)
        scanf("%d", &a[b]);
    
    /*This part, help me with this*/
      for (b = 0; b < 5; b++) {
        for (c = b + 1; c < 5++) {
          if (a > a[c]) {
            d = a;
            a = a[c];
            a[c] = d;
          }
        }
      }
    /*Until here*/
    
      for (b = 0; b < 5; b++) {
        printf("\n%d\n", a[b]);
      }
      getche();
    }
    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.

  6. #6
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    And in the tutorial section of this site you find what I think you are looking for...

    Bubble Sort and Modified Bubble Sort - Cprogramming.com

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Personally I (for once) prefer the wikipedia explanation: https://en.wikipedia.org/wiki/Bubble_sort because it has pretty pictures.

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    228
    Besides the picture, you can also try to sort a small array like
    Code:
    {5, 2, 10}
    by hand so you can see how the process works. Many times has this help me see what the program is doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand how to do
    By EspoirBondo in forum C Programming
    Replies: 7
    Last Post: 04-30-2013, 12:30 AM
  2. plz help me understand this
    By Farnaz in forum C# Programming
    Replies: 3
    Last Post: 07-06-2011, 11:15 AM
  3. help me understand...
    By stormfront in forum C Programming
    Replies: 2
    Last Post: 10-12-2005, 10:47 AM
  4. Help me out understand what I need to do please?
    By seal in forum C Programming
    Replies: 8
    Last Post: 10-11-2005, 10:31 PM
  5. Can't understand FAQ
    By Vorkosigan in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2004, 03:37 AM

Tags for this Thread