Selection sort in C || Arrays || C programming
In this, we are going to see a program in which we will Sort the array in the ascending order using selection sort method in C Programming Language.

//Sorts the array in the ascending order using selection sort method.

#include <stdio.h>
void main()
{
    int i, j, k, data, exchange;
    int x[50], n;
    printf("Enter how many numbers you want to sort out of 50: ");
    scanf("%d", &n);
    printf("Enter %d numbers to sort in asccending order:\n", n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &x[i]);
    }

    for (i = 0; i < n - 1; i++)
    {
        exchange = 0;
        k = i;
        data = x[i];
        for (j = i + 1; j < n; j++)
        {
            if (x[j] < data)
            {
                k = j;
                data = x[j];
                exchange = 1;
            }
        }
        if (exchange)
        {
            x[k] = x[i];
            x[i] = data;
        }
    }

    printf("The sorted array is....\n");
    for (i = 0; i < n; i++)
    {
        printf("\t%d", x[i]);
    }
}

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post