
Transcript
Remember when you learned long division in middle school? Not to knock math teachers, but long division was a slow, painful way to get to an answer — even though, eventually, you did. Then in high school, your math teacher finally said it was OK to just use a calculator. You probably grabbed your calculator and never looked back. All that time you spent carrying numbers and finding the remainder? Sayonara!
And if you ever bugged your teachers to explain why you’d spent so many hours learning long division when you could have just used a calculator, they probably told you how important it was to learn the basics so you’d understand more complicated functions down the road.
The same logic applies when we learn sorting algorithms.
end of transcript
This lesson covers the “long division” of basic sorts: bubble sorts and insertion sorts.
Your math teachers would agree they’re like long division, because:
Bubble and insertion sorts are almost never used in programming. You’re much more likely to use quick or merge sorts in the programs and languages you work with — they’re kind of like the calculators of algorithms.
We learn these basic sorts, however, because they’re simple and intuitive. And by learning these algorithms, you’ll understand the more complicated sorts we’ll describe in a future lesson.
Bubble sorts put values in order by iterating through a data set and comparing neighboring numbers. When the sort encounters a smaller value before a greater value, it swaps the values. The sort continues through the data set, value by value, swapping them until the array is properly sorted.
Here’s the basic process of the bubble sort algorithm:

Transcript
Let’s use a bubble sort to sort this array of playing cards from smallest to largest. We’ll start at the beginning.
10 is bigger than the Queen, so we’ll keep them. Then we’ll move on to the next one. The Queen is bigger than the 7, so we can swap them.
We’ll keep moving across the cards, swapping cards, so that the larger cards move to the right and the smaller ones to the left.
end of transcript
Is bubble sort a stable or unstable sort?
What’s the Big O worse case space efficiency for bubble sort?
O(1)O(N)O(log(N))What is the Big O worst case time efficiency for bubble sort?
O(1)O(N)O(N^2)O(Log(N))Next up is insertion sort. Basically, an insertion sort takes each item and inserts it into the right place in the array.
Like bubble sort, insertion sort isn’t terribly efficient — it ends up traversing over and over the array many times. However, it’s definitely faster than bubble sort and works more efficiently in a wider variety of circumstances. It’s mainly used in real life in conjunction with bucket sort, which we’ll learn about soon.

Transcript
One way to visualize insertion sort is to think of playing a game of cards.
Imagine you’re playing poker. As the dealer hands you your cards, you’re picking up them one by one and ordering them numerically as you go.
The first card you pick up is sorted by the fact that it’s the only card you have. Any subsequent cards you pick up can be ordered according to how they fit into your existing hand. Each time you pick up a new card, you can simply insert it into the proper location.
The next card is 2. It’s lower than your existing cards, so you can move it to the left. You’ll continue sorting cards this way as you pick them up.
end of transcript
Is insertion sort a stable or unstable sort?
What’s the Big O worst case space complexity for insertion sort?
O(1)O(N)O(Log(N))What’s the Big O worst case time efficiency for insertion sort?
O(1)O(N)O(N^2)O(Log(N))Despite bubble sorts being virtually unusable, interviewers love to ask about them. It’s the simplest sorting algorithm to explain and shows that you understand sorting conceptually. You might be asked to sketch out how bubble sort works or answer some basic questions about the algorithm, like these ones here. Similarly, you might be asked to sketch or explain how insertion sort works. Here’s a nice recap.
Luckily, the internet is full of fun visualizations on how sorting algorithms work! Check out a few:
