Following Program Sort The Array Elements Using Selection Sort :
#include<iostream>
using namespace std;
int a[100];
void min(int a[],int k,int n)
{
int j,i,min=a[k];
for(i=k+1;i<n;i++)
{
if(min>a[i])
{
min=a[i];
j=i;
a[j]=a[k];
a[k]=min;
}
}
}
main()
{
int k,n;
cout<<"Enter The Number Of Elements ";
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(k=0;k<n;k++)
{
min(a,k,n);
}
cout<<"Sorted Array is\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Output:
#include<iostream>
using namespace std;
int a[100];
void min(int a[],int k,int n)
{
int j,i,min=a[k];
for(i=k+1;i<n;i++)
{
if(min>a[i])
{
min=a[i];
j=i;
a[j]=a[k];
a[k]=min;
}
}
}
main()
{
int k,n;
cout<<"Enter The Number Of Elements ";
cin>>n;
cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(k=0;k<n;k++)
{
min(a,k,n);
}
cout<<"Sorted Array is\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Output: