SORT-P1

Nearly in order

Practice 1 of 6Sorting7 tests

The problem

Write insertion sort. Sort the list a in place: change the list you were given, and return nothing.

a = [29, 10, 14, 37, 13, 5, 21]
insertion_sort(a)
a    # [5, 10, 13, 14, 21, 29, 37]

One test is a list of 100,000 values that is nearly in order: no value is more than 3 places from where it belongs. Lesson 1 explains why insertion sort is fast there and selection sort is not. sorted() and .sort() are switched off.

Limits

  • Up to 2,000 values in any order, or up to 100,000 values that are nearly in order.

Builds on SORT-1 Selection, insertion and bubble sort.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1MixedNot run
2ReversedNot run
3Already sortedNot run
4RepeatsNot run
5Empty and oneNot run
62,000 valuesNot run
7100,000 values, nearly in orderNot run