Selection sort

Speed

About algorithm

Explanation

Selection Sort repeatedly finds the minimum element from the unsorted part and moves it to the beginning. It's simple but inefficient for large datasets.

Steps

  1. Start from the first element.
  2. Find the smallest element in the unsorted part of the array.
  3. Swap it with the current element.
  4. Move the boundary of the sorted part one step forward.
  5. Repeat until the array is sorted.

Pseudocode

selectionSort(arr):
  n = len(arr)
  for i from 0 to n - 1:
    minIndex = i
    for j from i + 1 to n - 1:
      if arr[j] < arr[minIndex]:
        minIndex = j
    swap arr[i] with arr[minIndex]

Complexity

Time: O(n²)

Space: O(1)