C Program to Sort Array Elements Using Selection Sort Algorithm - C Program

C Program to Sort Array Elements Using Selection Sort Algorithm

The selection sort algorithm sorts an array by repeatedly finding the minimum element (for ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

#include <stdio.h>
int main()
{
 int num[5],size=5,i,j,temp,min_index;
 
 printf("enter %d elements of array : ",size);
 for(i=0;i<size;i++) 
    scanf("%d",&num[i]);
 for(i=0;i<size-1;i++)
 {
     min_index=i;
	for(j=i+1;j<size;j++)
	{
	       if(num[min_index]>num[j])
	           min_index=j;
	}
	if(i!=min_index)
	{
			temp=num[i];
			num[i]=num[min_index];
			num[min_index]=temp;
	}
 }


 printf("Array Element After Sorting....\n");
 for(i=0;i<size;i++)
	printf("%d ",num[i]);

 return 0;
}


Output - C Program to Sort Array Elements Using Selection Sort Algorithm

Enter 5 elements of array : 4 2 5 1 7   
Array Element After Sorting....       
1 2 4 5 7  
Compile and Run Online