C program to sort an array using selection sort algorithm
PROGRAM:
#include <stdio.h>#include <stdlib.h>
void selection_sort(int [],int);
int main()
{
int *a;
int n,i;
printf("\nEnter the no. of elements\n");
scanf("%d",&n);
a=malloc(n*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selection_sort(a,n);
printf("Sorted Array:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n");
return 0;
}
void selection_sort(int a[],int n)
{
int i,j,minimum,temp;
for(i=0; i<n-1; i++)
{
minimum=i;
for(j=i+1; j<n; j++)
{
if(a[j]<a[minimum])
{
minimum=j;
}
}
//swapping
temp=a[i];
a[i]=a[minimum];
a[minimum]=temp;
}
}
OUTPUT:
Enter the no. of elements
5
Enter the elements
10
50
20
30
40
Sorted Array: 10 20 30 40 50