Algorithm of Selection Sort
| Step 1: | Repeat up to Step 5 for I = 0 to N-1 |
| Step 2: | MIN = a [I] POS=I |
| Step 3: | Repeat step 4 for J=I+1 to N |
| Step 4: | If a [J] < MIN then MIN =a [J] POS = J |
| Step 5: | TEMP = a [I] a [I] = a [POS] a [POS] = TEMP |
Program of Selection Sort
#include<stdio.h>
#include<conio.h>
#define N 5
void main()
{
void selection(int *a);
int a[N],i;
clrscr();
printf("Enter Elements in array\n");
for(i=0;i<N;i++)
{
printf("Enter Value of A[%d]:",i);
scanf("%d",&a[i]);
}
selection(a);
printf("Sorted List\n");
for(i=0;i<N;i++)
{
printf("A[%d]=%d\n",i,a[i]);
}
getch();
}
void selection(int *a)
{
int i,j,temp,min,pos;
for(i=0;i<N-1;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<N;j++)
{
if(a[j]<min)
{
min=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}