C Program for Selection Sort

Write a Program to Sort Array using Selection Sort in C with a practical example.

C Program for Selection Sort using For Loop

This program uses Nested For Loop to sort the array elements using selection sort.

/* C Program for Selection Sort */
#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < number; i++) {
        for(j = i + 1; j < number; j++) {
            if(a[i] > a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    printf("\n Selection Sort Result : ");
    for(i = 0; i < number; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
C Program for Selection Sort 1

Here, in this selection sort program, the For Loop will make sure that the number is between 1 and maximum size – 1.
First For Loop – First Iteration: for(i = 0; 0 < 4; 0++)
The condition is True so, it will enter into second for loop

Second For Loop – First Iteration: for(j = 1; 1 < 4; 1++) – This condition is True
if(a[0] > a[1]) => (10 > 9)  – This condition is True
temp = 10
a[i] = a[j] => a[0] = 9
a[j] = temp => a[1] = 10
Now the array will be  9 10 0 11.

Second For Loop – Second Iteration: for(j = 2; 2 < 4; 2++) – True
if(a[0] > a[2]) => (9 > 0)  – Condition is True
temp = a[0] => temp = 9
a[0] = a[2] => a[0] = 0
a[2] = temp => a[2] = 9
Now the array will be  0 10 9 11.

Do the same for the remaining Iterations of the second for loop. Next, repeat the same with first C Programming for loop.

Selection Sort Program

In this selection sort in c program, we used a slightly different approach to demonstrate the selection sort.

/* C Program for Selection Sort */
#include <stdio.h>
int main()
{
    int a[100], number, min, i, j, temp;
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < number - 1; i++) {
        min = i;
        for(j = i + 1; j < number; j++) {
            if(a[min] > a[j]) {
                min = j;
            }
        }
        if(min != i)
        {
            temp = a[i];
            a[i] = a[min];
            a[min] = temp;
        }
    }
    printf("\n Selection Sort Result : ");
    for(i = 0; i < number; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}

C selection sort output

Please Enter the total Number of Elements  :  5

 Please Enter the Array Elements  :  10 2 19 0 10

 Selection Sort Result :  0 	 2 	 10 	 10 	 19 	

Selection Sort Program using While Loop

In this program, we replaced the for loop with While Loop.

/* C Program for Selection Sort */
#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);
    
    i = 0;
    while( i < number) {
        j = i + 1;
        while( j < number) {
            if(a[i] > a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
            j++;
        }
        i++;
    }
    printf("\n Selection Sort Result : ");
    for(i = 0; i < number; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
 Please Enter the total Number of Elements  :  7

 Please Enter the Array Elements  :  20 0 25 19 4 22 16

 Selection Sort Result :  0 	 4 	 16 	 19 	 20 	 22 	 25 	

C Selection Sort Program using Functions

This selection sort program uses Functions to separate the logic, and Pointers to swap and sort array elements by using this selection sort.

/* C Program for Selection Sort */
#include <stdio.h>
void Swap(int *x, int *y) {
    int Temp;
    Temp = *x;
    *x = *y;
    *y = Temp;
}
void selectionSort(int a[], int number) {
    int i, j;
    for(i = 0; i < number; i++) {
        for(j = i + 1; j < number; j++) {
            if(a[i] > a[j]) {
                Swap(&a[i], &a[j]);
            }
        }
    }
}
int main()
{
    int a[100], number, i;
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);
    
    selectionSort(a, number);
    printf("\n Selection Sort Result : ");
    for(i = 0; i < number; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
Please Enter the total Number of Elements  :  6

 Please Enter the Array Elements  :  12 3 0 22 19 -8

 Selection Sort Result :  -8 	 0 	 3 	 12 	 19 	 22