Wednesday, 10 September 2014

SORTING IN ASCENDING ORDER

//CODE

#include<stdio.h>
int main()
{
 int a[10],i,n,pass,tmp=0;
 printf("\n Enter the size of the array:");
 scanf("%d",&n);
 printf("\n Enter the elements of the array:\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 pass=1;
 while(pass<n)
 {
  for(i=0;i<n-pass;i++)
  {
   if(a[i]>a[i+1])
   {
    tmp=a[i];
    a[i]=a[i+1];
    a[i+1]=tmp;
   }
  }
  pass=pass+1;
 }
 printf("\n The sorted array in ascending order is as follows:\n");
 for(i=0;i<n;i++)
 {
  printf("%d \n",a[i]);
 }
 getch();
 return 0;
}
//OUTPUT
Enter the size of the array:5
Enter the elements of the array:
46
79
82
43
16
The sorted array in ascending order is as follows:
16
43
46
79
82
 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.