C program to sort array in ascending order

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

yashan has written 70 articles

39 thoughts on “C program to sort array in ascending order

    1. Alyssa says:

      for(j=0;j<n-i;j++) there is a bug, because when j=n-i-1, which is the maximum value of j, j+1=n-i and there is no value for ar[j+1]. In this case ar[j+1] will be a random number chosed by the computer. The correct answer should be for(j=0;jar[j+1]) into if(ar[j]<ar[j+1]) and then we will find the bug.

  1. saadat says:

    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++

  2. sumon says:

    can any one give me the code of implement heap,delete element from heap,find the maximum and minimum number from the heap this code must in array implement.

  3. Akshay Immanuel says:

    Its not j<(n-i) but j<(n-1). If not for this change,you will be accessing array out of bounds. Try executing for n=3 and check. The loop will execute even for j=2 when i=0 and access array[3]

  4. Amina Areeba says:

    This program show the garbage value when print the data after sorting and please upload the program which sort the data from the start array last index

  5. LastTerra says:

    I have noticed you don’t monetize your blog, don’t waste your traffic, you can earn additional
    cash every month because you’ve got hi quality content. If you want
    to know how to make extra bucks, search for: Mertiso’s tips best
    adsense alternative

  6. BestShana says:

    I see you don’t monetize studystreet.com, don’t
    waste your traffic, you can earn additional
    bucks every month with new monetization method.
    This is the best adsense alternative for any type of website
    (they approve all sites), for more info simply search in gooogle:
    murgrabia’s tools

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>