Sort array in ascending order

Write a program in C language which accepts integer values from user, sorts it in ascending order and prints the integers in ascending order.

Output:

You may also like...

28 Responses

  1. pavan55 says:

    what is the logic for this example?

  2. LearnCOnline says:

    Logic here is very simple.
    Take the input in an array.
    then compare the first array element with others. If the first array element is greater than any of the other array element then swap it. The swapping will cause the smallest element to be placed at 1st position.
    Now, compare the second array element with others and not the previous one (1st Array element). If second array element is greater than any of the other array element then swap. If its value is less then no need to swap.
    This swapping will again cause second smallest element to be placed at position 2.

    If you still have any query, then kindly reply. 🙂

  3. BINIL BABY says:

    can u explain me further briefly…?

  4. Anonymous says:

    This is probably the easiest and the best method of doing it…..

  5. dhalla says:

    why have you taken another variable j??
    cant it be solved only with i??

  6. why is this in DESCENDING??

  7. Anonymous says:

    what if the input is -5, 98, 23, 91, 34 ??
    98 and 23 will be swapped
    98 and 91 will be swapped
    34 and 98 will be swapped
    and in the end the result will be
    -5,23,91,34,91
    which is not in order
    am i right ??

  8. Anonymous says:

    urgently need help.

    when i am running the above code, i get this error
    Task 10.c:2:18: fatal error: conio.h: No such file or directory

    how do i solve it?

  9. nepp says:

    Tnx for this example

  10. Anonymous says:

    I think the code mentioned below is more efficient.e.g.—If there are 5 no’s, we need only 4 pass in each of the for loop.But in your code there are 5 passes..i.e–one extra pass .we dont need that .
    I may be wrong.whats your opinion, admin?

    code–

    for( i=0;i{
    for( j=i+1;j{

  11. Anonymous says:

    #include<stdio.h>
    #include<conio.h>
    void main()

    {

    int a[100], i, n, s, j;

    printf(“Enter size of array\t”);

    scanf(“%d”, &n);

    printf(“Enter the elements of array\t”);

    for(i=0;ia[i+1])

    {

    s=a[i];

    a[i]=a[i+1];

    a[i+1]=s;

    }

    }

    }

    printf(“Array in ascending order\n”);

    for(i=0;i
    {

    printf(“%d< ", a[i]); } printf(“\n”); }

  12. Anonymous says:

    y is clrscr; function is used when not using functions…

  13. Anonymous says:

    The logic is simple….here he used arr[i] and arr[j] for an array itself…in it he compared two different elements of an array through i and j…..

  14. Anonymous says:

    Sir please said starting to end of program flow.

  15. Anonymous says:

    sir tell me full flow of program

  16. Anonymous says:

    sir i don’t know how can you used two for loops again you used one for loop

  17. Anonymous says:

    what if we want to sort the 2 d array?

  18. lyle says:

    hi. This code is really helpful but can you help me. I need to print first the original array content before it prints the sorted array. Thanks

    • Prashant N. says:

      Hi Iyle,

      It is great to hear that this website helped you. If you liked it, share it. 🙂

      Please find below the program that will first print the original array content and then the sorted array.

      Thanks,
      LearnCOnline Team

  19. PROSPER says:

    this is really helpful….thanks!

  20. ayan says:

    this is code for acs and des both

    #include
    #include
    void main()
    {clrscr();
    int array[5],des[5],acs[5];
    int temp;
    int i;
    for (i=0;i<5;i++)
    {
    printf("enter number: ");
    scanf("%d",&array[i]);

    }

    for(int l=0;l<5;l++)
    {
    des[l]=array[l];
    acs[l]=array[l];
    }

    printf("\n\ndes order:");
    for(i=0;i<4;i++)
    {
    for(int j=i+1;j<5;j++)
    {if(des[i]<des[j])
    {temp=des[i];
    des[i]=des[j];
    des[j]=temp;}
    }
    }
    for(int a=0;a<5;a++)
    {printf("\n%d",des[a]);}

    printf("\n\nacs order:");
    for(i=0;i<4;i++)
    {
    for(int j=i+1;jacs[j])
    {temp=acs[i];
    acs[i]=acs[j];
    acs[j]=temp;}
    }
    }
    for(a=0;a<5;a++)
    {
    printf("\n%d",acs[a]);
    }

    getch();
    }

  21. input=90,19,-78,83,-31,-44-11,84,-93,66
    output=-78,90,-31,19,-44,83,-11,84,-93,66

  22. baby says:

    1. Write a complete C program that allows the user to do the following (in sequence)

    i. Step 1 : Choose type one of the following Option :
    a. Option 1 : Sorting The Number in Ascending Order
    b. Option 2 : Sorting The Number in Descending Order
    c. Option 3 : Find Biggest No
    d. Option 4 : Find Smallest No
    e. Option 5 : Calculate The Average of All Number
    f. Option 6 : Calculate The Median Number
    g. Option 7 : Exit System

    ii. Step 2 : If user choose Option 1

  23. pavithra says:

    i accept all those cammands ..bt the doubt is whn we comepare the last 2 numbers means i will not wrk…so am totaly confused abt that ..any one can xplain me plz..

  24. sarojini says:

    pls explaim swapping in asecending order c program..

  25. ravi says:

    can anyone write program to sort 2 dimensional array plzz……

  1. December 19, 2014

    […] PDF File Name: Program to sort array in ascending order | c program Source: http://www.learnconline.com

Leave a Reply

Your email address will not be published. Required fields are marked *