Program to sort array using Selection Sort in c

Program to sort array using Selection Sort

Program to sort array using Selection Sort

#include<stdio.h>
#include<conio.h>

void main()
{
    int array[20], n, i, j, pos, temp;
    clrscr();

    printf("Enter total number of elements :");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("\nEnter element number %d : ", i+1);
        scanf("%d", &array[i]);
    }

    for (i = 0; i < (n - 1); i++)
    {
        pos = i;
        for (j = i + 1; j < n; j++)
        {
            if (array[pos] > array[j])
                pos = j;
        }
        if (pos != i)
        {
            temp = array[i];
            array[i] = array[pos];
            array[pos] = temp;
        }
    }

    printf("\nSorted elements :");

    for (i = 0; i < n; i++)
        printf("%d\n", array[i]);

    getch();
}

Output:-

Enter total number of elements :11

Enter element number 1 : 54

Enter element number 2 : 189

Enter element number 3 : 445

Enter element number 4 : 178

Enter element number 5 : 456

Enter element number 6 : 258

Enter element number 7 : 02

Enter element number 8 : 80

Enter element number 9 : 885

Enter element number 10 : 1100

Enter element number 11 : 786

Sorted elements :2
54
80
178
189
258
445
456
786
885
1100
That's all in this post, we will talk on the next topic in the next post, to read the full chapter of c language, click on the link and start reading.
c language Tutorial

 
Previous articleScope rule in function of python & LGB Rule(Full details)
Next articlePython function arguments (object, Keys,Default)

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here