Selection Sort

This type of sorting is called "Selection Sort" because it works by repeatedly element. It works as follows: first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.

The program is written below:

#include<iostream>
using namespace std;
int main()
{
int a[50];
int i,j,k,n,min,temp,l,pos;
cout<<"nEnter the size of the array(max 50)"; cin>>n;
cout<<"n Enter the array";
for(i=1;i<=n;i++) { cin>>a[i];
}
for(k=1;k<=n;k++)
{
min=a[k];
for(l=k+1;l<=n;l++) { if(min>a[l])
{
min=a[l];
pos=l;
}
}
temp=min;
a[pos]=a[k];
a[k]=temp;
}
for(int j=1;j<=n;j++)
{
cout<<a[j]<<"t";
}
return 0;
}

Output:
Screenshot from 2013-09-29 22:50:22

Leave a Reply