Selection Sort c Program

  • by

Selection Sort program in c language:
This is an example of c program to sort an array using Selection Sort Algorithm. The elements of array are entered by the user. Selection sort c program is explained properly with logic and example.

C program for Selection Sort is used to sort array in ascending order.You can also sort the array in descending order by modifying the condition in the loop.

Selection Sort Algorithm in c

This explains step-by-step the logic how Selection Sort in c language works.

  1. Selection sort algorithm starts by comparing first two elements of the array ,i.e., 1st element and 2nd element of the array.
  2. If 1st element is greater than the 2nd element then swap (or interchange) the elements ,i.e., 1st element will come in place of 2nd element and 2nd element will come in place of 1st element.
    If 1st element is smaller than the 2nd element then do not swap elements.
  3. Now, 1st and 3rd elements are compared and if 1st element is greater than 3rd element then swap them otherwise do not swap.
  4. This process will continue untill 1st element and last elements of the array are compared. This is the first iteration or first loop of selection sort algorithm.

If there are n elements in the array then there will be (n-1) iteration or loop in the selection sort algorithm.

To make Selection Sort Algorithm more efficient –

  • As the smallest element will be automatically placed at the first position in the array after 1st iteration. So, in 2nd iteration there is no need to compare 1st element with 2nd element and the second iteration starts by comparing 2nd with all other elements.
  • Similarly, in 3rd iteration there is no need to compare 3rd element with 1st and 2nd element. 

In (n-1)th iteration all the elements are already sorted. So, there is no need for nth iteration in the Selection Sort c program.

Selection Sort c program


/* Selection Sort in c */

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File          

int main()
{
    int a[20], n, i, j, t;

    clrscr(); // to clear output screen

    /* To take number of elements in an array from user */
    printf("Enter how many elements should be in the array: ");
    scanf("%d", &n);

    /* Array Input */
    for (i=0; i<n; i++)
    {
        printf(Enter array element %d", i+1);
        scanf("%d", &a[i]);
    }

    /* Array Sort (In Ascending Order) */
    for (i=0; i<n-1; i++)
    {
        for (j=i+1; j<n; j++)
        {
            if (a[i]>a[j])
            {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
       }

    /* Print the sorted array */
    printf("Sorted Array - ");

    for (i=0; i<n-1; i++)
    {
        printf("%d\t", a[i]);
    }

    getch(); //to hold output screen
    return(0); 
}

Output –
Enter how many elements should be in the array: 5
Enter array element 1: 40
Enter array element 2: 30
Enter array element 3: 10
Enter array element 4: 50
Enter array element 5: 20
Sorted Array - 10   20   30   40   50 

Selection Sort C Program Explanation

Here, Selection Sort in c is explained using an image as it becomes easy to understand. So, the below image properly explains the First iteration of Selection Sort in c.

sort array using selection sort c program

Selection Sort c program using Function

Here, all the code for Selection Sort c program is written in a user-defined function void selection_sort(int) and this function is called in main().


/* Selection Sort Algorithm */

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File 

/* function declaration */
void selection_sort(int);         

int main()
{
    int n;

    clrscr(); // to clear output screen

    /* To take number of elements in an array from user */
    printf("Enter how many elements should be in the array: ");
    scanf("%d", &n);

    /* calling function */
    selection_sort(n);

    getch(); //to hold output screen
    return(0);  
}  
    
void selection_sort(int n);
{
    int a[20], i, j, t; 

     /* Array Input */
    for (i=0; i<n; i++)
    {
        printf(Enter array element %d", i+1);
        scanf("%d", &a[i]);
    }

    /* Array Sort (In Ascending Order) */
    for (i=0; i<n-1; i++)
    {
        for (j=i+1; j<n; j++)
        {
            if (a[i]>a[j])
            {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
     }

    /* Print the sorted array */
    printf("Sorted Array - ");

    for (i=0; i<n-1; i++)
    {
        printf("%d\t", a[i]);
    }
}
Enter how many elements should be in the array: 5
Enter array element 1: 40
Enter array element 2: 30
Enter array element 3: 10
Enter array element 4: 50
Enter array element 5: 20
Sorted Array - 10   20   30   40   50 

Selection Sort Time Complexity in c

In Selection Sort c program Time Complexity, we will explain the Worst case, Average case and Best case.

Selection sort time complexity for both Best case and Worst case is same O(n2) as the number of comparisons are same in both cases.

Note:
Selection sort c program is easy to implement for sorting array elements. As the logic remains same for all languages, Selection Sort algorithm can be used in any language to sort array elements.