This is one of the most simple sorting techniques.....
Here we just check if the previous element in the array is greater than the i th element and replace it ..
#include<iostream>
using namespace std;
int main()
{
int n,i,j,temp,a[10];
cout<<"Enter array size : ";
cin>>n;
cout<<"\nEnter Array Elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n\nUnsorted Array Is : \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j+1]=a[j];
a[j]=temp;
}
}
}
cout<<"\n\nSorted Array Is : \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Here we just check if the previous element in the array is greater than the i th element and replace it ..
#include<iostream>
using namespace std;
int main()
{
int n,i,j,temp,a[10];
cout<<"Enter array size : ";
cin>>n;
cout<<"\nEnter Array Elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n\nUnsorted Array Is : \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j+1]=a[j];
a[j]=temp;
}
}
}
cout<<"\n\nSorted Array Is : \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
0 comments:
Post a Comment