Selection sort
Computer Science · Algorithms
Question 1 of 10
0Selection sort (ascending) on [37, 24, 2, 39]. Pass p looks at every item from position p - 1 to the end, finds the smallest, and swaps it into position p - 1. A swap happens only when the smallest item is not already in place. What is the value at index 0 (counting from 0) after pass 1?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Selection sort explained
Selection sort puts a list into order by repeatedly finding the smallest item that is still unsorted and moving it into place. It is simple to follow and is a common first sorting method in school courses.
The list is split into a sorted part on the left, which starts empty, and an unsorted part on the right. Each pass does the following:
- Search the unsorted part for its smallest item.
- Swap that item with the first item of the unsorted part.
- The sorted part grows by one item and the unsorted part shrinks by one.
The process repeats until the unsorted part is empty. For a list of items, the number of comparisons is , which grows as . Selection sort makes the same number of comparisons whatever the order of the input, and it makes at most swaps.
A common mistake is to think selection sort shifts items along to make room, as insertion sort does. It does not shift items. It finds the minimum and swaps it into place.
Worked example
Sort [7, 3, 9, 1] into ascending order.
- Pass 1: the smallest item is 1. Swap it with 7 to give [1, 3, 9, 7]. This takes 3 comparisons.
- Pass 2: the smallest unsorted item is 3, which is already in place, so there is no swap. This takes 2 comparisons.
- Pass 3: the smallest of 9 and 7 is 7. Swap them to give [1, 3, 7, 9]. This takes 1 comparison.
- In total there are comparisons and 2 swaps. The formula gives , which matches.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
Selection sort (ascending) on [4, 24, 12, 8, 35]. Pass p looks at every item from position p - 1 to the end, finds the smallest, and swaps it into position p - 1. A swap happens only when the smallest item is not already in place. What is the value at index 3 (counting from 0) after pass 1?
Medium
Selection sort (ascending) on [3, 8, 2, 1, 23]. Pass p looks at every item from position p - 1 to the end, finds the smallest, and swaps it into position p - 1. A swap happens only when the smallest item is not already in place. In pass 3, which value is moved into position 2 (counting from 0)? Use the array as it stands at the start of pass 3.
Hard
Selection sort (ascending) on [7, 22, 2, 19, 16, 12, 11]. Pass p looks at every item from position p - 1 to the end, finds the smallest, and swaps it into position p - 1. A swap happens only when the smallest item is not already in place. How many swaps does selection sort make from pass 1 to the last pass?