Friday 8 April 2016

WAP to sort elements in ascending order

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a[10],i,j,n,temp,k,ptr;
 cout<<"\n Enter size of array :";
 cin>>n;
 cout<<"\n Enter element of array :";
 for(i=1;i<=n;i++)
 cin>>a[i];
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
 while(temp<a[ptr])
 {
  a[ptr+1]=a[ptr];
  ptr--;
 }
  a[ptr+1]=temp;
}
 cout<<"\n The sorted elements are :\n";
 for(i=1;i<=n;i++)
 cout<<"    "<<a[i];
 getch();
 clrscr();
}
/* OUTPUT

  
 Enter size of array    :7

 Enter element of array :15 23 10 22 48 75 12

 The sorted elements are        :
    10    12    15    22    23    48    75

No comments:

Post a Comment