Back to list

Selection Sort

Repeatedly finds the minimum element from the unsorted part and puts it at the beginning.

Initial ArrayItems: 8
67
0
83
1
97
2
81
3
85
4
84
5
92
6
41
7
Sorted
Min Value
Scanning
Speed
Comp
0
Swaps
0
Array Controls

Time Complexity

All Cases
O(n²)

Selection sort always scans the unsorted part to find the minimum, taking quadratic time even if already sorted.

Space Complexity

Auxiliary SpaceO(1)

Sorts in-place.

Algorithm Details

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.

How it works

The algorithm maintains two subarrays in a given array.

  1. The subarray which is already sorted.
  2. Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

Complexity

  • Time Complexity: O(n²)
  • Space Complexity: O(1)