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];
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size-i;j++)
{
if(a[j+1]<a[j])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
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