Data Structure Selection Sort

program of selection sorting is given below:

//Selection Sorting
#include<stdio.h>
void main()
{
    int B[50],n,temp,min;
    printf("Enter the size of array :");
    scanf(" %d",&n);
    printf("Enter the array:\n");
    for(int i=0;i<n;i++)
    {
        scanf(" %d",&B[i]);
    }
    for(int i = 0;i<n-1;i++)
    {
        min = i;
        for(int j =i+1;j<n;j++)
        {
            if(B[j]<B[min])
            {
                min = j;
            }
        }
        if(min!=i)
        {
            temp = B[i];
            B[i]=B[min];
            B[min]=temp;
        }
    }
    printf("The sort array is :");
    for(int i=0;i<n;i++)
    {
        printf(" %d",B[i]);
    }
}

Output

Enter the size of array :4
Enter the array:
11
34
5
21
The sort array is : 5 11 21 34

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin