Output :
Enter the no. of elements:5
Enter the 5 elements :15 25 85 95 0
Elements after Sorting :0 15 25 85 95
Code for Bubble Sort:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int *a;
int i=0,j=0,n,b=0;
cout<<"Enter the no. of elements:";
cin>>n;
a=(int*)malloc(sizeof(int*)*n);
cout<<"Enter the "<<n<<" elements :";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
{
b=a[j+1];
a[j+1]=a[j];
a[j]=b;
}
}
}
cout<<"\nElements after Sorting :";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return(0);
}
Enter the no. of elements:5
Enter the 5 elements :15 25 85 95 0
Elements after Sorting :0 15 25 85 95
Code for Bubble Sort:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int *a;
int i=0,j=0,n,b=0;
cout<<"Enter the no. of elements:";
cin>>n;
a=(int*)malloc(sizeof(int*)*n);
cout<<"Enter the "<<n<<" elements :";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
{
b=a[j+1];
a[j+1]=a[j];
a[j]=b;
}
}
}
cout<<"\nElements after Sorting :";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return(0);
}
No comments:
Post a Comment
we are hear to discuss your queries ,so please feel free to ask any of your queries.