Sorting the numbers in Ascending order : C Programming

Q. Write a program to sort the numbers in ascending order which is entered by the user using C programming language. 

==> Code goes like this :
#include<stdio.h>
int main()
{
    int i,j,num,a[50],temp;
    printf("Enter size of array:");
    scanf("%d",&num);
    printf("Enter %d elements to sort:",num);
    for(i=0;i<num;i++)
    {
        scanf("%d",&a[i]);
    }                           // Taking values till now
    for(i=0;i<num-1;i++)
    {
        for(j=i+1;j<num;j++)
        {
            if(a[i]>a[j])           // > for ascending
            {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            }
        }
    }                            // swapping values for sorting
    printf("\n The ascending order is:");
    for(i=0;i<5;i++)
    {
        printf(" %d",a[i]);    // Displaying values
    }
return 0;   
}

Output : 













Tracing down the whole logic,
size of arry : 5 which means we can enter 5 elements only

for loop : to enter 5 values

3          5         4         2        6
a[0]    a[1]    a[2]    a[3]    a[4]

i         j   
0    0+1=1    3>5, false, j increments and becomes 2

0    0+2=2    3>4, false, j increments and becomes 3

0    0+3=3    3>2, true, swaps value of i and j and order becomes,

2           5        4        3         6

a[0]    a[1]    a[2]    a[3]    a[4]  again, j increments and becomes 4

i        j
0    0+3-4    2>6, false, j increments and becomes 5 exceeds j<num

Second iteration of i,
Value positions,
2            5        4       3         6
a[0]    a[1]    a[2]    [a3]    a[4]

i         j
1    1+1=2    5>4, true, swaps value of i and j and element becomes

2           4         5       3         6
a[0]    a[1]    a[2]    [a3]    a[4]  and j increments

i        j
1    1+2=3    4>3, true, value of i and j and element becomes

2           3        5        4        6
a[0]    a[1]    a[2]    [a3]    a[4]  and j increments

i        j
1    1+3=4    3>6, false, j increments and becomes 5, exceeds j<num

Third iteration of i,
Value positions,
  2         3         5       4         6
a[0]    a[1]    a[2]    [a3]    a[4]

i        j
2    2+1=3    5>4, true, swaps value of i and j and becomes

   2        3         4        5         6    j increments
a[0]    a[1]    a[2]    [a3]    a[4]

i         j
2    2+2=4    3>6, false, j increments, exceeds j<num

Fourth iteration of i,
i        j
3    3+1=4, 5>6, false, j increments, exceeds j<num

i increments and becomes 4 which exceeds i<num-1 and loop terminates

and final element order remains,

   2        3        4         5        6    which is the ascending form of 3, 5, 4, 2, 6
a[0]    a[1]    a[2]    [a3]    a[4]

I have used Dev CPP to compile this program.
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you 






Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment