Friday 27 April 2018

C Program to Perform Selection Sort

Program :-


#include<stdio.h>
int A[100];
int findmin(int n,int start)  //Entering the function to find minimum
{
int minpos,i;  //Assigning values
minpos=start;
i=start+1;
while(i<n)  //comparing starts
{
if(A[i]<A[minpos])
minpos=i;
i++;
}
return minpos;  //Returning minimum position
}
void selsort(int n)  //Entering the function to perform Selection sort
{
int t;  //Assigning values
int minpos;
int pass=0;
while(pass<(n-1))  //Passing starts
{
minpos=findmin(n,pass);  //Finding minimum position of the smallest element
t=A[minpos];  //Swapping starts
A[minpos]=A[pass];
A[pass]=t;
pass++;
}
}
void main()  //Entering main function
{
int i,n;
printf("Enter the array size");  //Reading the array size
scanf("%d",&n);
printf("Enter the array elements");  //Reading the array elements
for(i=0;i<n;++i)
scanf("%d",&A[i]);
selsort(n);  //Call funtion to perform selection sort
printf("The sorted array is ");  //Printing sorted array
for(i=0;i<n;++i)
printf("%d\t ",A[i]);
}

Output :-

Eg 1:-
Enter the array size : 5
Enter the array elements  : 32 45 18 25 56
The sorted array is : 18 25 32 45 56





No comments:

Post a Comment

How to Invert an SVG image using CSS ?

You can invert your svg easily by using following css code svg { -webkit-filter: invert(100%); /* safari 6.0 - 9.0 */ filter...