Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone

Selection Sort Program in C Programming | DSA Tutorials

YASH PAL, 8 May 20268 May 2026

To program the selection sort algorithm in C programming, we first need to understand how selection sort works. The selection sort starts from the first element and searches the entire list until it finds the minimum value. The sort places the minimum value in the first place, selects the second element and searches for the second smallest element. The process continues until the complete list is sorted.

A selection sort is one in which successive elements are selected in order and placed into their proper sorted positions. The selection process needs to be done only from 1 to n-1 rather than up to n. Analysis of the selection sort is straightforward. The first pass makes n-1 comparisons, the second pass makes n-2 and so on. So total comparisons are (N-1) + (N-2) + (N-3) + … + 2 + 1 = N (N-1)/2 = O(N2).

If you want a working example of the selection sort algorithm with a step-by-step explanation, then read this tutorial – Selection Sort Algorithm Example.

Let’s see the two different implementations of the selection sort algorithm in C programming.

Selection sorting (First logic) Program in C

#include<stdio.h>
#include<conio.h>
#define SIZE 10

void selection_sort(int [], int);

void main()
{
  int a[SIZE],i,n;
  printf("Enter how many elements ");
  scanf("%d",&n);
  /*Input Array*/
  
  for(i=0;i<n;i++)
  {
    printf("Enter element %d ",i+1);
    scanf("%d",&a[i]);
  }
  
  selection_sort(a,n);
  /*Output Array */
  
  for(i=0;i<n;i++)
      printf("%d",a[i]);
  
  getch();
}

void selection_sort(int a[], int n)
{
    int i,j,t,min,minpos;
    
    for(i=0;i<n-1;i++)
    {
        min = a[i];
        minpos = i;
        
        for(j=i+1; j<n; j++)
        {
            if(a[j] < min)
            {
                min = a[j];
                minpos = j;
            }
        }
        if( i != minpos)
        {
           t = a[i];
           a[i] = a[minpos];
           a[minpos] = t;
        }
     }
}

Output

Enter how many elements 6
Enter element 1 200
Enter element 2 140
Enter element 3 50
Enter element 4 60
Enter element 5 90
Enter element 6 77
50 60 77 90 140 200 

Selection sorting (Second Logic) C Program

#include<stdio.h>
#include<conio.h>
#define SIZE 10

void selection_sort(int [], int);

void main()
{
    int a[SIZE],n,i;
    printf("Enter how many elements ");
    scanf("%d",&n);
    
    /*Input array */
    
    for(i=0;i<n;i++)
    {
        printf("Enter element %d ",i+1);
        scanf("%d", &a[i]);
    }
    
    selection_sort(a,n);
    
    /*Output array */
    
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
    
    getch();
}

void selection_sort(int a[], int n)
{
    int minpos;
    int i,j,t;
    
    for(i=0;i<n-1;i++)
    {
       minpos = i;
       for(j=i+1; j<n;j++)
       {
           if(a[minpos] > a[j])
               minpos = j;
       }
       
       if(minpos != i)
       {
           t = a[i];
           a[i] = a[minpos];
           a[minpos] = t;
       }
    }
}

Output

Enter how many elements 5
Enter element 1 99
Enter element 2 88
Enter element 3 77
Enter element 4 66
Enter element 5 55
55 66 77 88 99 

Data Structures & Algorithms Tutorials for Beginners
Data Structures Tutorials DSA Tutorials

Post navigation

Previous post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes