You are on page 1of 2

LAB PROGRAM

/* program to sort N numbers using selection sort */


#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,j,a[10],pos,temp;
clrscr();
printf("Enter the size:\t");

/*read the size */

scanf("%d",&n);
printf("\nEnter %d elements to sort:\n",n); /*read the elements */
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\tGiven Elements\n");

/*display the elements */

printf("---------------------------------");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
/*selection sort- sorting in ascending order*/
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)

{
if(a[j]<a[pos])
pos=j;
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
/* display the sorted elements */
printf("\n\tThe sorted elements are:");
printf("\n--------------------------------------");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
getch();
}

You might also like