

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Sorting Algorithms
incomplete
2: Bubble Sort
incomplete
3: Bubble Sort Big O
incomplete
4: Why Bubble Sort?
incomplete
5: Merge Sort
incomplete
6: Merge Sort Big O
incomplete
7: Why Merge Sort?
incomplete
8: Insertion Sort
incomplete
9: Insertion Sort Big O
incomplete
10: Why Use Insertion Sort?
incomplete
11: Quick Sort
incomplete
12: Quick Sort Big O
incomplete
13: Fixing Quick Sort
incomplete
14: Why Use Quick Sort?
incomplete
15: Selection Sort
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Insertion sort builds a sorted list one item at a time. It's much less efficient on large lists than merge sort because it's O(n^2), but it's actually faster (not in Big O terms, but due to smaller constants) than merge sort on small lists.
Click to play video
Our influencers want to sort their affiliate deals by revenue. None of our users have more than a couple hundred affiliate deals, so we don't need an n * log(n) algorithm like merge sort. In fact, insertion_sort can be faster than merge_sort, and uses less of our server's memory.
Complete the insertion_sort function according to the given pseudocode:
j variable to the current indexj is greater than 0 and the element at index j-1 is greater than the element at index j:
j and j-1j by 1In some languages you need to use a temp variable to swap values, but in python you can do that in a single line:
a = 5
b = 3
a, b = b, a
print(a)
# 3
print(b)
# 5