selection sort


Summary

Selection sort

  • comparison based
  • iterative
  • in-place
  • not stable

Time complexity

Loop invariant

  • at the kth pass, the first k elements will be sorted into their final positions

Concept

Algorithm

  1. start with the lower index at the first index
  2. select the smallest value in the array, by comparison
  3. swap it into the lowest index
  4. increment the lowest index and repeat 2-3 to find the next smallest
  5. repeat 2-3 until the lowest index is the last index
java
for (int i = 0; i < N-1; ++i) { // N-1 iterations
  cur_min = i;  
  for (int j = i+1; j < n; ++j)  
    if (A[j] < A[cur_min])
      cur_min = j;  
  swap(A, i, cur_min);  
}

“select” the current smallest