Program to sort elements in Ascending Order using Selection Sort in C language
Selection Sorting
Definition:- It compare the array of number with the rest of number in the array list & takes the selected to the top of the list & again compare another second lowest number & so on until we get the sorted number array.
//Header Files
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, k, pass, min, arr[20]; //variables
clrscr(); //to clear screen
// User to input the limit of the array
printf("\n Enter the number limit:-");
scanf("%d",&n); // To scan the limit of the array
//Asking user to input the elements of the array
printf("\n Enter the element to be sorted:->\n");
for(i=0;i<n;i++)
scanf("%d", &arr[i]); //Scanning the elements in the array
for(pass=0; pass<n-1;pass++)
{
min=pass;
for(j=pass+1;j<n;j++)
{
if(arr[min]>arr[j]) //Comparing array elements
min=j;
}
if(min!=pass) // verifying the elements
{ //Swapping the elements
k=arr[pass];
arr[pass]= arr[min];
arr[min]=k;
}
}
printf("\n Element for Selection sort are:-");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]); //Printing the Sorted array
}
getch();
}
Output:-
Enter the number limit:-6
Enter the element to be sorted:->
9
4
6
2
0
1
Element for Selection sort are:-
0
1
2
4
6
9
0 Comment(s)