Thread: Program to print the numbers in ascending order

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    Program to print the numbers in ascending order

    I need help to understand below program. How do we find ascending order manually ? How does for loop work in this program?
    Code:
    /* TO PRINT THE NUMBERS IN ASCENDING ORDER USING ARRAYS */
    
    
    #include <stdio.h>
    
    int main(void)
    {
        int a[10], i=0, j=0, n, t;
    
        printf ("\n Enter the no. of elements: ");
        scanf ("%d", &n);
        printf ("\n");
    
        for (i = 0; i <n; i++)
        {
            printf ("\n Enter the %dth element: ", (i+1));
            scanf ("%d", &a[i]);
        }
    
        for (j=0 ; j<(n-1) ; j++)
        {
            for (i=0 ; i<(n-1) ; i++)
            {
                if (a[i+1] < a[i])
                {
                    t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }
        }
    
        printf ("\n Ascending order: ");
        for (i=0 ; i<n ; i++)
        {
            printf (" %d", a[i]);
        }
    
          /* indicate successful completion */
          return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The idea is that if two elements are out of order, you swap them. The loops are there to mainly make sure that enough swaps happen.

    The worst case for bubble sort, an array that is in reverse order from sorted, is actually instructive, since it will mimic the sort written down very closely.
    Start with the first two, swap if they are bold. Proceed to the next two numbers. If the numbers are fine, proceed without a swap.
    5 4 3 2 1
    now the array is: 4 5 3 2 1
    4 5 3 2 1
    4 3 5 2 1
    4 5 3 2 1
    4 5 2 3 1
    4 5 2 3 1
    4 5 2 1 3
    The first two are technically in order, go to next swap.
    4 5 2 1 3
    4 5 1 2 3
    Again, first two are in order.
    4 5 1 2 3
    4 1 5 2 3
    4 1 5 2 3
    4 1 2 5 3
    4 1 2 5 3
    4 1 2 3 5
    4 1 2 3 5
    1 4 2 3 5
    1 4 2 3 5
    1 2 4 3 5
    Again, we have a lot of elements in order.
    1 2 4 3 5
    1 2 3 4 5
    It's called bubble sort because the smaller items gradually move, bubbling up, to the front of the array.

    And that is basically bubble sort. Wikipedia has a nice animation of a bubble sort happening.
    Last edited by whiteflags; 10-17-2017 at 10:40 PM.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Thanks for your quick response. I understand for loop test limited conditions. I need help to understand below part of program. I don't understand how does this part of program work ?

    Code:
        for (j=0 ; j<(n-1) ; j++)
        {
            for (i=0 ; i<(n-1) ; i++)
            {
                if (a[i+1] < a[i])
                {
                    t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    This part will swap two numbers.
    Code:
            if (a[i+1] < a[i])
            {
                t = a[i];
                a[i] = a[i + 1];
                a[i + 1] = t;
            }
    This is the "if two numbers are out of order, you swap" part. The idea is, if you iterate over n elements, swapping the ones out of order, n times, you are guaranteed to have a sorted result.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    OK. There are two for loop. What does they do ?

    Code:
    for (j=0 ; j<(n-1) ; j++)
    {
        for (i=0 ; i<(n-1) ; i++)
        {
    
            
    
           
    
        }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abhi143
    There are two for loop. What does they do ?
    The inner loop loops over the entire array (other than the last element, because on each iteration, the current element is compared with the next element, so you cannot loop until the last element because that element has no next element). The effect of this is to move the elements a little closer to the sorted order, e.g., if the largest value was stored in the first element, it would be "pushed" all the way back to be stored in the last element. Hence, one idea is to run the inner loop many times until the array is sorted, and one way (though not the most efficient way) of doing that is to always run the entire inner loop almost as many times as there are elements in the array, therefore you have the outer loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arranging Random Numbers in Ascending Order
    By csmith03 in forum C Programming
    Replies: 9
    Last Post: 10-16-2017, 03:06 PM
  2. Replies: 3
    Last Post: 02-23-2012, 08:30 AM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. Program to print 3 numbers in ascending order
    By jadedreality in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 07:32 PM
  5. Arranging numbers in ascending order
    By Aero in forum C++ Programming
    Replies: 7
    Last Post: 12-22-2001, 07:52 AM

Tags for this Thread