Sorting Algorithm : Selection Sort

Ajay krishnan
4 min readJun 14, 2021

Selection sort is an in-place comparison sorting algorithm. It has an O(n²) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort.

Worst complexity :

Average complexity :

Best complexity :

stable : No

Algorithm

Idea

For every iteration we find the minimum element and swap it
Example:
step 1 : Extract minimum element in the array and swap it with element at index 0 , now we have minimum element at index 0
step 2 : Extract minimum element from the remaining list of elements (look for minimum element in the index locations 1 to end ) and swap it with element at index 1
step 3 : Extract minimum element from the remaining list of elements (look for minimum element in the index locations 2 to end ) and swap it with element at index 2
………………. continue till the end of iteration

Complete code link : Selection sort

Selection sort in C language
Selection sort in C++
Selection sort in Java
Selection sort in JavaScript
Selection sort in Python

Selection Sort in C language

We accomplish Selection sort by creating two utility methods , one gets the minimum index position during every iteration and second helps us swap elements.
We can use Iterative or Recursive implementation , here it’s done in both ways

Click this link to run Selection Sort in C language

This utility function will look for minimum element in the array , it looks between the index represented by start and stop
once we find the minimum we can swap elements using this utility function
Selection sort algorithm iterative version
Selection sort algorithm recursive version

Selection Sort in C++

Click this link to run Selection Sort in C++

This utility function will look for minimum element in the array , it looks between the index represented by start and stop
once we find the minimum we can swap elements using this utility function
Selection sort algorithm iterative version
Selection sort algorithm recursive version

Selection Sort in Java

Click this link to run Selection Sort in Java

This utility function will look for minimum element in the array , it looks between the index represented by start and stop
once we find the minimum we can swap elements using this utility function
Selection sort algorithm iterative version
Selection sort algorithm recursive version

Selection Sort in JavaScript

Click this link to run Selection sort in JS

This utility function will look for minimum element in the array , it looks between the index represented by start and stop
once we find the minimum we can swap elements using this utility function
Selection sort algorithm iterative version
Selection sort algorithm recursive version

Selection Sort in Python

Click this link to run Selection sort in python

This utility function will look for minimum element in the array , it looks between the index represented by start and stop
once we find the minimum we can swap elements using this utility function
Selection sort algorithm iterative version
Selection sort algorithm recursive version

Resource

--

--