Skip to content

C program to sort array in ascending order

by yashan on March 28th, 2012

Program in the C programming language to sort all the elements in the array in the ascending order. This programs uses the bubble sort method to sort the numbers in ascending order.

Definition Bubble Sort- Bubble sort is a simple sorting technique in which passes are used and after each pass larger no. obtains it’s sorted position. To sort the elements ‘n-1‘ passes are used where n is the total number of elements.

/* C program to arrange or sort the array in the ascending order */
#include<stdio.h>
#include<conio.h>
void main()
{
     int ar[100],j,n,i,tmp;
     
     printf(" Enter the size of the array \t");
     scanf("%d",&n);
     
     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     
     printf("\n Array is - ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     
     for(i=0;i<n;i++)
     {
           for(j=0;j<n-i;j++)
           {
                 if(ar[j]>ar[j+1])
                 {
                       tmp=ar[j];
                       ar[j]=ar[j+1];
                       ar[j+1]=tmp;
                 }
           }
     }
     
     printf("\n\n Array in the ascending order is - \n");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     getch();
}

Input -
Enter the size of the array – 5
Array is – 23     65    10    45    34
Output -
Array in the Ascending order is – 10    23    34    45    65

23 Comments
  1. Umar permalink

    Make it abit easy and understandable

  2. nikita permalink

    make it easy study material for students…

  3. Vicky permalink

    thanks….i was in need of this program….i got it now

  4. Hello ! I am, John paul Dengo. and i need to learn morrow about C & C++. Tha’s why i’m interesting to you.

  5. dharani permalink

    thanks i got the prgm easily frm this site

  6. tarzan permalink

    there is a bug
    for(j=0;j<n-i;j++) === wrong

    for(j=0;j<n-1;j++)==right

    • ASHWIN KAIN permalink

      well, n-1 is wrong there’s no bug, prog. is simple and easy.

    • khwaja permalink

      for n-i its printing -2 garbage value at the begining in case of static arrays …for dynamic arrays its fine..
      for static n-1 is correct.

  7. Dinesh Kiran permalink

    Thanks…

  8. I’m little satisfied on searching wth this site

  9. don’t worry i will gave’s part and how you can implement you’r program by using c++ i’m also a student and i have alred implement a simple program achuly i need to know more about c++

  10. Parashuram permalink

    Please give me programs on an array daily one to my ewail id. Thanks

  11. falesh kumar permalink

    Thank you for help

  12. priyanka permalink

    this program is a grt help to me

  13. santosh ojha permalink

    give atleast one question answer and i,e.,complete

  14. Harsha permalink

    iz really helpful……….

  15. aviketh permalink

    soooooooooo helpful……….

  16. fahad permalink

    Itz very helpful……….

  17. Εncore un sublime article : j’en discuterai dans la semaine avec mes voisins

  18. Very Helpful article while array size is user defined

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS