Program to sort the elements of an array using Selection sort

Program (Without Function)
Time Complexity: O(n2)

#include <stdio.h>
int main()
{
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
    printf("*  WAP to sort the elements of an array using Selection sort  *\n");
    printf("*                     Created by Sheetal Garg                 *\n");
    printf("*                       Assistant Professor                   *\n");
    printf("*                       Phone No:9467863365                   *\n");
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
    int a[10], n, i, j, position, swap, min;
    printf("\nEnter number of elements\n");
    scanf("%d", &n);
    printf("Enter Numbers\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < n - 1; i++)
    {
        position = i;
        min = a[i];
        for (j = i + 1; j < n; j++)
        {
            if (a[j] < min)
            {
                position = j;
                min = a[j];
            }
        }
        if (position != i)
        {
            swap = a[i];
            a[i] = a[position];
            a[position]=swap;
        }
    }
    printf("\nSorted Array:\n");
    for (i = 0; i < n; i++)
        printf("%d\t", a[i]);
    return 0;
}

Output

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  WAP to sort the elements of an array using Selection sort  *
*                     Created by Sheetal Garg                 *
*                       Assistant Professor                   *
*                       Phone No:9467863365                   *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Enter number of elements
8
Enter Numbers
12
21
23
32
34
43
45
54

Sorted Array:
12      21      23      32      34      43      45      54

Program (Using Function)

#include <stdio.h>
void SelSort(int array[], int n);
int main()
{
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
    printf("*    WAP to sort array using Selection sort using functions   *\n");
    printf("*                     Created by Sheetal Garg                 *\n");
    printf("*                       Assistant Professor                   *\n");
    printf("*                       Phone No:9467863365                   *\n");
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
    int array[10], n, i;
    printf("\nEnter number of elements\n");
    scanf("%d", &n);
    printf("Enter Numbers\n");
    for (i = 0; i < n; i++)
        scanf("%d", &array[i]);
    SelSort(array, n);
    return 0;
}
void SelSort(int array[], int n)
{
    int i, j, position, swap, min;
    for (i = 0; i < (n - 1); i++)
    {
        position = i;
        min = array[i];
        for (j = i + 1; j < n; j++)
        {
            if (min < array[j])
            {
                min = array[j];
                position = j;
            }
        }
        if (position != i)
        {
            swap = array[i];
            array[i] = array[position];
            array[position] = swap;
        }
    }
    printf("\nSorted Array is:\n");
    for (i = 0; i < n; i++)
        printf("%d\t", array[i]);
}

Output

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    WAP to sort array using Selection sort using functions   *
*                     Created by Sheetal Garg                 *
*                       Assistant Professor                   *
*                       Phone No:9467863365                   *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Enter number of elements
7
Enter Numbers
23
32
12
21
23
34
43

Sorted Array is:
43      34      32      23      23      21      12
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.