A+DS

Selection Sort

Find the minimum, put it in place. Repeat.

Like picking the shortest person from a line and moving them to the front, then the next shortest, and so on. Selection Sort scans the unsorted portion to find the minimum, swaps it into the next sorted position, and repeats.

  sorted | unsorted
  [     ] | [ ?  ?  ?  ?  ?  ]
              scan →→→→→
              find MIN!

  [ MIN ] | [ ?  ?  ?  ?  ]  swap to front
  [ ✔ ✔ ] | [ ?  ?  ?  ]  repeat...

How it works

  1. 1Assume the first unsorted element is the minimum
  2. 2Scan the rest to find the actual minimum
  3. 3Swap the minimum into the next sorted position
  4. 4Move the sorted boundary one step right

Loading...