Selection sort in C

In this article, you’ll learn about the selection sort algorithm and its implementation program in the C programming language with and without a function in easiest possible way.

Selection sort algorithm in C

Selection sort is faster than the bubble sort as the number of comparisons taking place is minimized.

In selection sort algorithm, we select a number according to the requirement and place it at the end of the list in one pass. One complete traversal of the array/list is known as a pass. This process continues till the array is sorted completely in the required order.

For example, if you want to arrange data in ascending order, the largest element is selected and placed at the end of the list. In the next pass, the second largest element is selected and placed second last. This process is repeated until all the elements are placed in the required order.

The best part is, with each pass the number of elements to be traversed are reduced by 1 as one element is already placed in the proper position. You see here? That’s why this is faster than the bubble sort.

Let us try to understand selection sort in C from an example.

Program: Selection sort program in C

This is a C program to sort N numbers in ascending order using selection sort, where N is the number of elements in the array and is entered by the user.

// Selection sort program in C
#include <stdio.h>
int main()
{
    int array[10];
    int i, j, k, num, temp;
    printf("Enter the number of elements: ");
    scanf("%d", &num);
    printf("Enter the elements of the array: ");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("\nInput array is:\n");
    for (i = 0; i < num; i++)
        printf("%d\t", array[i]);
    for(i=0;i<num-1;i++)
    {
        temp=array[i];
        k=i;
        for(j=i+1;j<num;j++)
        {
            if(array[j]<temp)
            {
                temp=array[j];
                k=j;
            }

        }

        array[k]=array[i];
        array[i]=temp;
    }
    printf("\nArray after selection sort:\n");
    for (i = 0; i < num; i++)
        printf("%d\t", array[i]);
    printf("\n");
    return 0;
}

Output: Sort numbers in ascending order using selection sort

Enter the number of elements: 8
Enter the elements of the array: 82 93 47 72 35 31 19 47

Input array is:
82      93      47      72      35      31      19      47
Array after selection sort:
19      31      35      47      47      72      82      93

The selection sort algorithm can be written separately to understand it properly using function. Below is a selection sort example to implement the same.

Program: Selection sort in C using function

This C program uses the selection sort algorithm inside a function to sort numbers in descending order. The size and elements(numbers) of the array are entered by the user.

//Selection sort program in C using function
#include <stdio.h>
//function prototype
int selectionsort(int[], int);
int main()
{
    int array[10];
    int i, j, k, num, temp;
    printf("Enter the number of elements: ");
    scanf("%d", &num);
    printf("Enter the elements of the array: ");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("\nOriginal array is:\n");
    for (i = 0; i < num; i++)
        printf("%d\t", array[i]);
    printf("\n\nArray in decreasing order after selection sort:\n");
    selectionsort(array,num);
    printf("\n");
    return 0;
}

// function definition
int selectionsort(int arr[], int size)
{
    int temp, i, j, k;
    for(i=0;i<size-1;i++)
    {
        temp=arr[i];
        k=i;
        for(j=i+1;j<size;j++)
        {
            if(arr[j]>temp)
            {
                temp=arr[j];
                k=j;
            }

        }

        arr[k]=arr[i];
        arr[i]=temp;
    }
    for (i = 0; i < size; i++)
        printf("%d\t", arr[i]);

}

Output: Sorted array in decreasing order using selection sort

Enter the number of elements: 5
Enter the elements of the array: 54 89 6 327 45

Original array is:
54      89      6       327     45

Array in decreasing order after selection sort:
327     89      54      45      6

Also read:

In this article, you learned about the selection sort algorithm in C and its implementation program.

Challenge: Write C program to sort numbers in descending order using selection sort in the comments.

For more programming articles and code snippets, visit programming articles.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

Your email address will not be published. Required fields are marked *