Sunday, October 16, 2011

Selection Sort

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[100];
    int i=0,j=0,n,small,temp,pos;
    printf("Enter size of the array:");
    scanf(%d",&n);
    for(i = 0; i < n; i++)
    scanf("%d ", arr[i]);
    for(i =0; i <=n-1; i++)
    {
        small=a[i];
        pos= 0;
        for(j =i+1; j <=n-1; j++)
        if(a[j]<small)
        {
            small=a[j];
            pos= j;
        }   
        temp=a[pos];
        a[pos] = a[i];
        a[i]=temp;
    }
    printf("Sorted Array is:\n");
    for(i = 0; i <n; i++)
    printf("%d\n", a[i]);
    getch();
}

No comments:

Post a Comment