Write a C program to sort an array using selection sort.

#include<stdio.h>
#include<conio.h>
void SELECTION_SORT(int a[], int len)
{
int j,i;
int smallest,pos,start,temp;
for(i=0;i<len;i++)
{
start=i;                                                
smallest=a[start];                   
pos=start;                               
            for(j=start;j<len;j++)  
            {
                        if(smallest > a[j])       
                        {
                        smallest=a[j];             
                        pos=j;              
                        }
            }
temp=a[i];                                                  
a[i]=a[pos];                                         
a[pos]=temp;                                                  
}
printf("\nDISPLAYING SORTED ARRAY ");
for(i=0;i<len;i++)
{
            printf("%d ",a[i]);
}
}

void main()
{
int arr[10],n,i;
clrscr();
printf("\nENTER THE TOTAL ELEMENTS ");
scanf("%d",&n);
printf("\nENTER ARRAY ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
SELECTION_SORT(arr, n);
getch();

}

0 Comment "Write a C program to sort an array using selection sort."

Post a Comment