Selection sort repeatedly finds the minimum element from unsorted part and puts it at the beginning. O(n 2) average and worst. The selection sort minimizes the number of swaps, but the number of comparison is still high. For data that is already sorted or almost sorted, the insertion sort does better.

selection sort

What is selection sort?

Selection sort repeatedly finds the minimum element from unsorted part and puts it at the beginning. The time complexity is O(n 2) time.


Selection sort visualization

selection sort doodle


Selection sort code

Java

Javascript

Python

Doodle

selection sort doodle


Free download

Download Java, JavaScript and Python code
Algorithm types and algorithm examples