Q:

Understanding this C-based bubble sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int ctr, inner, outer, didSwap, temp;
    int nums[10];
    time_t t;

    srand(time(&t));

    for (ctr = 0; ctr < 10; ctr++)
    {
        nums[ctr] = (rand() % 99) + 1;
    } 
    puts("\nHere is the list before the sort:"); for (ctr=0; ctr < 10; ctr++) {
    printf("%d\n", nums[ctr]); }

    for(outer = 0; outer < 9; outer++) {
        didSwap = 0;
        for (inner = outer; inner < 10; inner++)
        {
            if (nums[inner] < nums[outer])
            {
               temp = nums[inner];
               nums[inner] = nums[outer];
               nums[outer] = temp;
               didSwap = 1;
            }
        }
        if (didSwap == 0)
        {
            break;
        }
    }
    puts("\nHere is the list after the sort:"); for(ctr = 0; ctr < 10; ctr++) {
    printf("%d\n", nums[ctr]);

    } 
    return 0; 
}

I don't understand this part:
for(outer = 0; outer < 9; outer++) {
    didSwap = 0;
    for (inner = outer; inner < 10; inner++) {
        if (nums[inner] < nums[outer])
        ...
    }
}

If outer = 0 and inner = outer then both inner and outer equal to 0. and if the loop FOR says if (nums[inner] < nums[outer])
then how can nums[0] can be smaller than nums[0] since both inner and outer = 0? Please help me to understand.

guys i think my text book code is faulty. what do you think?
now the prob is with the BREAK. Do u think its on a right place.

if (didSwap == 0) {
      break; }

now the problem is what if the 1st two array values are in ascending order and rest of the element of NUMS[] are random, then after the 1st iteration of inner loop it will break the outer loop since the didSwap will still be equivalent to zero.
i tried to initialized the NUMS[] manually like this..

int nums[10]={4,6,8,65,47,74,21,22,65,36};

please have a look...thanks

A:

1)
Here is pseudocode that will help you to understand:
FOR J=1 TO N-1 {
    FOR I=1 TO N-J { 
        IF A[I]>A[I+1] THEN SWAP A[I],A[I+1]
        NEXT I
    }
    NEXT J
}

2)
As for the Bubble sorting in process.. Here is a simple example:
1) At first we compare the first two elements.
     If the 1st el. is bigger (or equal) than the next el. - we swap them.
     If the 1st el. is smaller -  we do nothing. 
     (The  smallest elements will be closer to the top & biggets to the bottom)
   Then we compare 2nd and 3rd elements, them 3rd and 4th etc. 
   Compare all the elements until the last in array.
   /* In this cycle, the biggest element will go to the bottom. */

2) Then we "forget" the last (the biggest) element and repeat the same again.

3) Repeat 1) and 2) successively until the end. 
   /* After all, all the elements will be sorted now: */
   /* from the smallest to the largest.               */

EXAMPLE:
Suppose we have 4 elements: 8, 6, 2, 1. That's how we will sort them:

1st cycle:
  8, 6, 2, 1
  v  v
   8 is bigger than 6, so we swap them      

  6, 8, 2, 1
     v  v
     8 is bigger than 2, so we swap them      

  6, 2, 8, 1
        v  v
        8 is bigger than 1, so we swap them      

  6, 2, 1, 8 
           v 
          The biggest element is at the bottom now.

2nd cycle:
  6, 2, 1, 8 
  v  v
  6 is bigger than 2, so we swap them      

  2, 6, 1, 8 
     v  v
     6 is bigger than 1, so we swap them      

  2, 1, 6, 8 
        v  v
        6 will always be smaller or equal to 8, so we use ...
        for (inner = 0; inner < (N-outer); inner++)
                                 ^^^^^^^ ... this expression to avoid
                                                  unnecessary actions.

3rd cycle: 
  2, 1, 6, 8     
  v  v
  2 is bigger than 1, so we swap them      

The are 4 elements but we do (4-1)= 3 cycles:
    for(outer = 0; outer < (N-1); outer++)
                            ^^^

3)
Now, imagine that N = 10, outer = J and inner = I:
for(J = 0; J < (N-1) ; J++) {
    didSwap = 0;
    for (I = 0; I < (N-J); I++)
    {
        if (nums[I] < nums[I + 1])// at the beginning, here J = 0 and I = 1;
        {                         // then J = 0 and I = 2 etc.
           temp = nums[I];        // It compares the first element with the
           nums[I] = nums[I + 1]; // othe ones and swaps them so more lightweight
           nums[I + 1] = temp;    // (smaller, light) element will move higher.
           didSwap = 1;           // Just like a bubble.
        }
    }
    if (didSwap == 0)
    {
        break;
    }
}

UPDATE:
4)
You can't break the sorting loop until it finishes! 
Look at the following code. It does fit the Bubble-sorting pseudocode (at the top of this answer):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10   // Here you can change the number of elements that
               // will be sorted.

int main()
{
    int ctr, inner, outer, didSwap, temp;
    int nums[N];
    time_t t;

    srand(time(&t));

    for (ctr = 0; ctr < N; ctr++)
    {
        nums[ctr] = (rand() % 99) + 1;  // Filling the elements with random 
    }                                   // values from 1 to 99.

    puts("\nHere is the list before the sort:");
    for (ctr=0; ctr < N; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    didSwap = 0;
    for(outer = 0; outer < (N-1); outer++) {

        for (inner = 0; inner < (N-outer); inner++)
        {
            if (nums[inner] >= nums[inner + 1]) // notice that there is `>=` 
            {                                   // instead of `>`. 
               temp = nums[inner];              // This will exchange also
               nums[inner] = nums[inner + 1];   // equal elements so the
               nums[inner + 1] = temp;          // sorting will work correctly.

            }
        }
        didSwap = 1; // Change `didSwap` only once --> after all cycles
                     // and all swappings: changing it's value after each
                     // swapping is a waste of machine's resources.
    }                    

    /* I can't understand why do you want to use this variable, but here it is. */
    printf(" >>> didSwap = %d <<<\n", didSwap);  

    puts("\nHere is the list after the sort:");
    for(ctr = 0; ctr < N; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    return 0;
}

