💻 Computer Science · Topic 46 of 80

Selection Sort

Find the smallest item and move it to the front.

Interactive labWatch the algorithm sort step by step

About this topic

Selection sort sorts a list by repeatedly finding the smallest item in the unsorted part and swapping it into the next position at the front. The list is divided into a sorted section on the left and an unsorted section on the right, and the sorted section grows by one item each pass.

For example, with [64, 25, 12, 22], pass one finds the minimum 12 and swaps it with 64, giving [12, 25, 64, 22]. Pass two finds 22 and swaps with 25, giving [12, 22, 64, 25]. Pass three swaps 64 and 25, and the list is sorted. It always makes about n squared divided by 2 comparisons, so time is O(n squared) even if the data is already sorted, but it uses at most n minus 1 swaps.

This is helpful when swapping is costly. Like bubble sort, it is good for teaching but not for big data. A common misconception is that selection sort adapts to nearly sorted lists; it does not. Another mistake is swapping inside the search loop instead of after finding the smallest. Remember the idea: select the minimum, place it, repeat.

Key ideas

  1. Fewer swaps than bubble sort
  2. Still O(n²)
  3. Works in place

Quick check

Use cases: where Selection Sort helps

Computing ideas run phones, search engines, games and the wider internet.

Frequently asked questions

What is Selection Sort?

Find the smallest item and move it to the front.

What will I learn in Selection Sort?

Fewer swaps than bubble sort. Still O(n²). Works in place.

Is Selection Sort part of Computer Science?

Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.

Does the Selection Sort lab work on a phone or tablet?

Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.

How should I study Selection Sort?

Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.

Do I need an account, and is my progress saved?

No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.

More in Computer Science