All parts of Sorting .....
//Bubble Sorting in C++..
#include<iostream>
using namespace std;
int main()
{
int temp,i,j,n,a[10];
cout<<"Enter your array's size:"<<endl;
cin>>n;
cout<<"Enter your array"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"After sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
//Insertion in C++.....................
#include<iostream>
using namespace std;
int main()
{
int temp,i,j,n,k,a[10];
cout<<"Enter your array's size:"<<endl;
cin>>n;
cout<<"Enter your array"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"After selection sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
//Selection Sort in C++....
#include<iostream>
using namespace std;
int main()
{
int temp,i,n,j,k,a[10];
cout<<"Enter your array's size:"<<endl;
cin>>n;
cout<<"Enter your array"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
k=i;
if(a[k]>a[j])
{
k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
cout<<"After selection sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
No comments