Sort like a Boss!
4 elementary sorting algorithms
These are the 4 elementary sorting methods everyone must know.
No code? No problem. We will keep it visual.
Bubble Sort
Step 1: Moving from left two right, pick two cards at a time.
Step 2: Swap them if they are out of order.
Step 3: Do many passes until all cards are sorted.
Practice bubble sort at the dojo.
Insertion Sort
Step 1: Moving from left to right, pick each card.
Step 2: Move it to its proper position among others to its left.
Step 3: And so on, until you reach the end.
Practice insertion sort at the dojo.
Selection Sort
Step 1: Moving from left to right, pick each card.
Step 2: Then pick the best card from all the cards to its right.
Step 3: If second pick is better than the first pick, exchange.
Step 4: And so on, until you reach the end.
Practice selection sort at the dojo.
Merge Sort
Step 1: Divide your cards into two sets.
Step 2: Sort each set separately.
Step 3: Merge them back in order.
Practice merge sort at the dojo.
Which one should we use?
It depends.
- Firstly, never use Bubble sort, as it is the slowest.
- Use Selection or Insertion sorts for small data sets.
- Use Merge (and other types) of sorts for larger data sets.