Sort like a Boss!

4 elementary sorting algorithms

Rajaram Gurumurthi
2 min readOct 27, 2019

These are the 4 elementary sorting methods everyone must know.

No code? No problem. We will keep it visual.

Bubble Sort

“Bubble” up the cards slowly into their proper position.

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

“Insert” cards one at a time into their proper position.

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

“Select” the next best card, move it into its proper position, rinse and repeat.

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

Divide and conquer like a 👑 boss

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.

--

--

Rajaram Gurumurthi
Rajaram Gurumurthi

No responses yet