

![]()
Let A be the vector that needs sorting and n its length. The strategy is the following: at every step an element of the vector is placed on its final position. The first step implies finding A[k]= minimum {A[1]....A[n]} and interchanging it with A[1]. The i-th step implies finding A[k]=min{A[i]....A[n]} and interchanging it with A[i].
Pascal program:
var a:array[1..100] of integer;
min,n,i,j,k,m:integer;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=1 to n-1 do
begin
min:=a[i];
k:=i;
for j:=i+1 to n do
if a[j]<min then begin min:=a[j]; k:=j; end;
m:=a[k];
a[k]:=a[i];
a[i]:=m;
end;
for i:=1 to n do
write(a[i],' ');
writeln;
end.
C++ program:
#include<iostream.h>
void main()
{
int a[100],min,n,m,i,j,k;
cin>>n;
for (i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n-1;i++)
{
min=a[i];
k=i;
for(j=i+1;j<=n;j++)
if (a[j]<min){ min=a[j]; k=j;}
m=a[k];
a[k]=a[i];
a[i]=m;
}
for (i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
Time: O(n^2)