Program Code:
#include<iostream.h>
#include<conio.h>
main()
{
int a[50],size;
char ch;
cout<<"Enter size of array\n";
cin>>size;
cout<<"Enter the order of sorting\n";
cout<<"Enter a for ascending or d for decending\n";
cin>>ch;
cout<<"Enter elements of array\n";
for(int i=0;i<size;i++)
{
cin>>a[i];
}
int t;
for(int i=1;i<=size;i++)
{
t=a[i];
for(int j=0;j<i;j++)
{
if(t<a[j])
{
for(int k=i;k>=j;k--)
{
a[k]=a[k-1];
}
a[j]=t;
break;
}
}
}
cout<<"The sorted array is\n";
if(ch=='a')
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<"\t";
}
}
else if(ch=='d')
{
for(int i=size-1;i>=0;i--)
{
cout<<a[i]<<"\t";
}
}
getch();
}
Output:
0 comments:
Post a Comment