Description: A Program to illustrate selection sort in an array.
Author: Naman Kumar
#include<conio.h>
void main()
{
clrscr();
int i,j,smallest,pos,temp=0,a[20],n;
cout<<"enter the no of elements in array";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
smallest=a[i];
pos=i;
for(j=i+1;j<n;j++)
{ if(a[j]<smallest)
{ smallest=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
for(i=0;i<n;i++)
cout<<a[i]<<",";
getch();
}
Please LIKE & SHARE US if you find this helpful!
0 comments:
Post a Comment