Selection sort in C++
#include<iostream>
using namespace std;
int main()
{
int min,i,j,temp,a[100],n;
cout<<"\tSelection sort";
cout<<"\nEnter size of array=\t";
cin>>n;
cout<<"\nEnter actual array element:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
min=i;
for(j=i+i;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"\nResultant array=\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
Output:
Selection sort
Enter size of array= 5
Enter actual array element:
9
7
8
4
6
Resultant array=
4 6 7 8 9
Comments
Post a Comment