• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Selection Sort in C
Next →
← Prev

Selection Sort in C

By Dinesh Thakur

The Selection sort in C is a simple sorting algorithm used for sorting an array by repeatedly iterates. It first finds the smallest element from the unsorted list of elements, swaps with the first position element, finds the second smallest element, swaps with the second position element, this process continues till all the elements are sorted. This method based on the following principle:

Note: The element must never be touched and compared if it adds to the sorted part of the list.

1. Select the element with the least value.
2. Exchange it with the first element.
3. Then repeat the above-given operations with the remaining (n – 1) elements, then with (n – 2) elements, until only one element (the most considerable element) is left.

Suppose we want to sort an array A with N elements A[1], A[2],….., A[N] using selection sort in ascending order. The algorithm begins (Pass 1) by finding the smallest element’s location in the array of N elements and interchange it with element A[1]. This step places the smallest element in A[1] and leaves the rest of the array in unsorted order. Then in the next step (Pass 2), find the location of the smallest element in the subarray A[2], A[3], …..A[N] of size N-1. Interchange this smallest element with A[2], leaving the elements A[1], A[2] in order.
In general, in the Kth step (Pass K), find the location of the smallest element in the unsorted subarray A[K], A[K+ 1],…., A[N] of size N-K-1 and interchange this smallest element with A[K] so that elements A[1], A[2],……, A[K] are sorted. This process continues until the last step (Pass N-1), select the second largest element and swaps it with A[N-1], leaving the largest element at A[N].

Selection sorting is an in-place sorting algorithm, it does not require additional storage, but it requires auxiliary memory to store the data temporarily. The selection-sort algorithm has the same efficiency as the bubble-sort algorithm. 

The selection sort can utilize if memory space is limited. It’s because, unlike other sorting algorithms, selection sort doesn’t go about swapping elements until the array end, resulting in less temporary storage space used.

We’ll be covering the following topics in this tutorial:

  • The Advantages & Disadvantages of selection sort
  • Why selection sort is unstable?
  • Example of Selection Sort
  • Algorithm for Selection Sort:
  • Time Complexity of Selection Sort in C
  • Selection sort program in C

The Advantages & Disadvantages of selection sort

The main advantage of the selection sort is that it performs well on a small list. It is used to sort files with tremendous values and small keys. It is because the selection is based on keys and swaps only if necessary.

The selection sort’s primary disadvantage is its inefficiency when the input size increases, and its performance decreases than the insertion sort algorithm.

Selection Sort in C

Why selection sort is unstable?

The selection sort is not a stable sorting algorithm, because it finds the smallest element from the unsorted list of elements and swaps it with the element at current position.

Suppose the array is
6 3 4 9 5 6 7
Let’s distinguish the two 6’s as 6(a) and 6(b) .

So our array is:
6(a) 4 5 6(b) 3 7 9

After iteration 1:

3 will be swapped with the first position element:

So our array becomes:
3 4 5 6(b) 6(a) 7 9

Our array is in sorted order, and we see that 6(a) comes before 6(b) in the initial array but not in the sorted array.

So we can see that the selection sort is not stable.

Example of Selection Sort

We have an array {7, 5, 2, 1,8} the smallest element in this array is 1. Then we replace 1 in the first place, and after that, the array looks like {1, 5, 2, 7, 8}. Now, we’ll iterate and finds the smallest element again. We see the next smallest element is 2, swaps it with the second position array and scanning the array and finally, we get the sorted array as [1,2,5,7,8].

Algorithm for Selection Sort:

START
 Step 1 → Set smallest to the beginning
 Step 2 → Search the smallest element in the array
 Step 3 → swap the first element with the smallest element.
 Step 4 → assign the second element as smallest.
 Step 5 → Repeatedly iterates until we get a sorted array.
STOP

Time Complexity of Selection Sort in C

In Selection sort, the algorithm requires a minimum number of swaps, and in the best case it takes ZERO (0) swaps when the input is in the sorted array-like 1,2,3,4, The best, average, and worst-case complexities of the selection algorithm are O(n2) for sorting n elements. The selection sort is both the worst and the average case quadratic and does not require additional memory.

Worst case condition.

Worst case condition of sorting algorithms is when data is sorted in the reverse order. In selection sort when data is sorted in reversed order, the total number of comparisons

(n – 1)+(n – 2)+ ··· + [n – (n – 1)] = n(n – 1)- [1 + 2 + ··· + (n – 1)]
= n((n-1)/2)
T(n) = O(n2)

Let us take a look at the code for the the programmatic implementation.

Selection sort program in C

// C program for implementation of selection sort
#include<stdio.h>
int main() {
 int array[100], size, i, j, position, temp;
 printf("Enter Number of Elements : ");
 scanf("%d", &size);
 printf("Enter %d Numbers : ", size);
 for (i = 0; i < size; i++)
  scanf("%d", &array[i]);
  for (i = 0; i < (size - 1); i++) {
   position = i;
   for (j = i + 1; j < size; j++) {
    if (array[position] > array[j])
      position = j;
   }
   if (position != i) {
     temp = array[i];
     array[i] = array[position];
     array[position] = temp;
   }
 }
 printf("Sorted list in ascending order:\n");
 for (i = 0; i < size; i++)
   printf("%d\t", array[i]);
   return 0;
}

selection sort program in c

You’ll also like:

  1. Array C++ Selection Sort
  2. What is bubble sort in C with example?
  3. What is Insertion Sort in C with Example
  4. Merge Sort in C
  5. Java – JComboBox Selection Change Listener Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW