Header Ads Widget

C PROGRAM: Program to implement selection sort in C language - Sparsh Blog

 Ans:->





What do you mean by Selection Sort❓-

                          In Selection Sort, the smallest element is exchange with the first element of the unsorted list of elements then the second smallest element is exchanged with the second element of the unsorted list of elements and so on until all the elements get sorted. 



The program to implement selection sort in C language.


Program is as follows:--


#include <stdio.h>

int main()

{

        int array[100], n, c, d, position, t;

        printf("Enter number of elements\n");

        scanf("%d", &n);

        printf("Enter %d integers\n", n);

        for (c = 0; c < n; c++)

        scanf("%d", &array[c]);

        for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times

        {

                position = c;

                for (d = c + 1; d < n; d++)

                {

                        if (array[position] > array[d])

                        position = d;

                }

                if (position != c)

                {

                        t = array[c];

                        array[c] = array[position];

                        array[position] = t;

                }

        }

        printf("Sorted list in ascending order:\n");

        for (c = 0; c < n; c++)

        printf("%d\n", array[c]);

        return 0;

}


OUTPUT:----


Enter 5 integers

85

45

96

3

25


Sorted list in ascending order:


3

25

45

85

96


THANKU FOR READING THIS ARTICLE ON-



If you want to suggest any changes in the program please feel free to contact us in the comment section or you can reach us using Contact us Page.



Post a Comment

0 Comments