6. A) Write a C program to implement Selection Sort technique (ascending order). Trace the steps for the input: 9, 1, 6, 3, 2, 0, 5
Answer:
#include<stdio.h>
void main( )
{
int a[100],i,j,n,temp,pos;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers in unsorted order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if( a[pos]>a[j])
pos=j;
}
if(pos !=i )
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
output:
Enter the value of n
6
Enter the numbers in unsorted order:
9 1 6 3 2 0
The sorted array is
0
1
2
3
6
9