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

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

Loading...
இணைந்திருங்கள் 24 நேரமும்!! Like us on Facebook!)