Selection Sort Algorithm
Selection sort is a simplicity sorting algorithm. It works as its name as it is. Here are basic steps of selection sort algorithm:
1. Find the minimum element in the list
2. Swap it with the element in the first position of the list
3. Repeat the steps above for all remainder elements of the list starting at the second position.
Here is the selection sort algorithm implemented in C programming language:
#include <stdio.h>
#include <stdlib.h>
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void selection_sort(int list[], int n)
{
int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{
if (list[j] < list[min])
{
min = j;
}
}
swap(&list[i], &list[min]);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void main()
{
const int MAX_ELEMENTS = 10;
int list[MAX_ELEMENTS];
int i = 0;
// generate random numbers and fill them to the list
for(i = 0; i < MAX_ELEMENTS; i++ ){
list[i] = rand();
}
printf("The list before sorting is:\n");
printlist(list,MAX_ELEMENTS);
// sort the list
selection_sort(list,MAX_ELEMENTS);
// print the result
printf("The list after sorting:\n");
printlist(list,MAX_ELEMENTS);
}