Selection sort is an In place sorting algorithm and follows the following three steps to sort an array

If we are given an array of N items and L = 0(lower bound), Selection Sort will:

  1. Find the position X of the smallest item in the range of [LN−1]
  2. Swap X-th item with the L-th item
  3. Increase the lower-bound L by 1 and repeat Step 1 until L = N-2

Code

Complexity Analysis

Time Complexity — O(N²) Space Complexity — O(1)

Improved Selection Sort

If we go through the inner loop with no swapping at all, it means that the array is already sorted and we can stop Selection Sort at that point where it terminates in O(N) time.

I make simple iOS apps