Full Byte Developer.

Insertion sort Python code you can run

Run insertion sort Python code and watch its sorted prefix grow beside bubble and selection sort.

Published here · 2 sources

I built this insertion sort Python level so you can run it. ALGO ARCADE runs each level as one Python file [1].

Run insertion sort Python code

bash
python3 insertion_sort.py

You should see the default row become ordered.

Try an insertion sort Python example

bash
python3 insertion_sort.py 1 2 3 4 5

You should see each value stay in the sorted prefix.

Race insertion sort with the other sorting levels

The arcade races every current sorting level on the same shuffled 10,000-number row. It runs each plain sorting function three times and prints the median time [1].

bash
python3 arcade.py --race 10000

You should see Bubble sort, Selection sort, Insertion sort, Merge sort, Quick sort, and Heap sort with a time and extra-memory line.

Inspect the insertion sort algorithm

Insertion sort moves the next value left until its neighbor is no longer larger [2]. Bubble sort uses neighbor swaps. Selection sort chooses one smallest value per round. Find the rest in Dev explainers.

Send this to the friend who needs a runnable model of insertion sort.

Keep this guide

By Full Byte Developer

Filed under Dev explainers

Questions about this guide

How do I run insertion sort Python code on an ordered row?

Run python3 insertion_sort.py 1 2 3 4 5 from the ALGO ARCADE folder.

What is an insertion sort sorted prefix?

It is the left part of the row that insertion sort has already placed in order.

Sources

Next in this stack