Program to sort array using Selection Sort solution using c language--programming_info.problem3

Program to sort array using Selection Sort.

This above example is just a reference picture. don't
think it as an input of an array.
you can give your own input.

#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();
}
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support