5/01/2009

C Program Selection Sort


/*C Program Selection Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i,j,n,small,position;

    printf("Enter the number of elements:\n");
    scanf("%d",&n);
    printf("Enter the elements: \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(i=0; i<n-1; i++)
    {
        small=a[i];
        position=i;
        for(j=i+1; j<n; j++)
            if(small>a[j])
            {
                small=a[j];
                position=j;
            }
        a[position]=a[i];
        a[i]=small;
    }
    printf("Soretd list is: \n");
    for(i=0; i<n; i++)
        printf("%d \n",a[i]);

}
Out Put:
Enter the number of elements:
9
Enter the elements:
54
78
95
15
32
25
14
68
123
Soretd list is:
14
15
25
32
54
68
78
95
123

No comments:

Post a Comment