//Program to sort an array using selection sort in C++.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50], n, temp, k , loc, i;
int min, j;
clrscr();
cout<<endl<<"Enter the number of elements in array: ";
cin>>n;
cout<<endl<<"The array :";
for (i=0;i<n;i++)
{
cin>>a[i];
}
for (k=0; k<n-1; k++)
{
min=a[k];
loc=k;
for (j=k+1; j<n; j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
cout<<endl<<"The sorted array is: ";
for (i=0; i<n; i++)
{
cout<<a[i]<<endl;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50], n, temp, k , loc, i;
int min, j;
clrscr();
cout<<endl<<"Enter the number of elements in array: ";
cin>>n;
cout<<endl<<"The array :";
for (i=0;i<n;i++)
{
cin>>a[i];
}
for (k=0; k<n-1; k++)
{
min=a[k];
loc=k;
for (j=k+1; j<n; j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
cout<<endl<<"The sorted array is: ";
for (i=0; i<n; i++)
{
cout<<a[i]<<endl;
}
getch();
}