C Program for Selection Sort

In this tutorial, we will learn how to create a program in C that sorts an array using the selection sort technique. At last, we have also created a function that can be used to sort any given array in ascending order using the selection sort technique.

But before going through the program, if you are not aware of how selection sort works, refer to the step-by-step workings of selection sort. Now let's move on and implement it in a C program.

Selection sort in C

To sort an array in ascending order using the selection sort technique in C programming, you have to ask the user to enter the array's size and its elements. Then apply the selection sort mechanism and print the sorted array as output, just like the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int size, arr[50], i, j, temp, small, count=0, index;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        count=0;
    }
    printf("\nNow the Array after sorting is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the sample run after a successful build and run. This is the first snapshot of the sample run:

selection sort in c programming

Now enter the array size, say 5, and its elements as 54, 21, 8, 18, and 3, and press the ENTER key to see the sorted array shown in the second snapshot of the sample run provided here:

selection sort in c

Here is another sample run. This is the final snapshot:

c selection sort

C Selection Sort Program Explained

If you want to print array elements as a step-by-step sorting of array elements shown on the output screen along with its final array, Then you can modify the above code as shown below to print array elements after each sorting step:

#include<stdio.h>
#include<conio.h>
int main()
{
    int size, arr[50], i, j, temp, small, count=0, index;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        printf("\nStep %d: ", i+1);
        for(j=0; j<size; j++)
            printf("%d ", arr[j]);
        printf("\n");
        count=0;
    }
    printf("\nNow the Array after sorting is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c sort array using selection sort

C selection sort using a user-defined function

Let's create the same program, which is to sort an array in ascending order using the selection sort technique, but this time with the help of a function. Here selection sort is implemented inside a function named selsort() that uses two arguments, one of which is an array and the other is its size, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
void selsort(int arr[], int size);
int main()
{
    int size, arr[50], i;
    printf("Enter size for Array: ");
    scanf("%d", &size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    selsort(arr, size);
    printf("\nThe sorted Array is:\n");
    for(i=0; i<size; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}
void selsort(int arr[], int size)
{
    int i, j, temp, small, count=0, index;
    for(i=0; i<(size-1); i++)
    {
        small = arr[i];
        for(j=(i+1); j<size; j++)
        {
            if(small>arr[j])
            {
                small = arr[j];
                count++;
                index = j;
            }
        }
        if(count!=0)
        {
            temp = arr[i];
            arr[i] = small;
            arr[index] = temp;
        }
        count=0;
    }
}

Here is the final snapshot of the above program:

c selection sort using function

The concept used in the above program is similar to that used in the first program, except we have implemented the function selsort() here. This function implements all selection sorting techniques. We only have to pass any array and its size as an argument to this function. And all the elements inside the array get sorted in ascending order.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!