Monday, 20 February 2017

Selection Sort

In computer science, a selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

#include<stdio.h>
#define MAX 5
void main()
{
    int ar[MAX],i,j,tmp,small;
      for(i=0;i<5;i++)
    {
        printf("Enter number>> ");
        scanf("%d",&ar[i]);
    }
    printf("\nUnsoted Array\n");
    for(i=0;i<MAX;i++)
        printf("%d\t",ar[i]);
    for(i=0;i<MAX-1;i++)
    {
        small=i;
        for(j=i+1;j<MAX;j++)
        {
            if(ar[small]>ar[j])
            {
                small=j;
            }
        }
        if(small!=i)
        {
            tmp=ar[i];
            ar[i]=ar[small];
            ar[small]=tmp;
        }

    }
    printf("\nsorted\n");
    for(i=0;i<MAX;i++)
        printf("%d\t",ar[i]);
}