Selection Sort
January 31, 2009 · Print This Article · Email It
Selection sort is one of the basic programs in C++ to sort an array. This comes in handy in sorting elements in a small program because of its ease of implementation and lack of risk factor. Selection sort isn’t much seen in the normal scenario but it is used for academic purposes and is a part of one of the important topics in the computer science syllabus of CBSE.
#include<iostream.h>
void main(){
int a[5],temp,small,pos;
for(int i=0;i<5;i++)
{
cin>>a[i];
}
for(i=0;i<4;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<5;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
cout<<"The sorted array is"<<endl;
for(int j=0;j<5;j++)
{
cout<<a[j];
}
}



