Selection Sort

FastSlow
Initializing...
Time Complexity
Best
O(n2)O(n^2)
Average
O(n2)O(n^2)
Worst
O(n2)O(n^2)
Space Complexity
O(1)O(1)
StabilityFALSE
In-placeTRUE

Explanation

What is Selection Sort?

Selection Sort is a simple sorting algorithm that divides the input list into two parts: a sorted sublist of items built up from left to right and a sublist of the remaining unsorted items that occupy the rest of the list.

Steps

  1. Find the smallest element in the unsorted part of the array.
  2. Swap it with the first element of the unsorted part.
  3. Move the boundary between the sorted and unsorted parts one element to the right.

Complexity

  • Best Case: O(n2)O(n^2)
  • Average Case: O(n2)O(n^2)
  • Worst Case: O(n2)O(n^2)

Pseudo Code

1procedure selectionSort(list)
2 for i from 0 to list.length - 2
3 minIdx = i
4 for j from i + 1 to list.length - 1
5 if list[j] < list[minIdx]
6 minIdx = j
7 swap(list[i], list[minIdx])
8end procedure
Sponsor Ad