What you need to know about sorting algorithms — JavaScript

Michael Mitrakos
InitJS

--

Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!

This is the introduction to a series on sorting algorithms in JavaScript. You might be wondering why should you learn all these algorithms when there’s already a few built-in ways to sort lists. The short answer is: because google and large tech companies want you to know them. The long answer is: depending on the list size, different sorting algorithms will have varying efficiencies that can make a major difference in your application. The time and space complexity of the algorithms you build are very important, and that’s what will get you paid the big bucks. So let’s jump in!

As you might have guessed, a sorting algorithm takes a list of items and sorts them— normally in alphabetical or numerical order.

Learning sorting algorithms is a great way to improve your craft and demand as a software engineer. These are a great way to practice time and space complexity analysis, and come up with clever solutions to simple problems. Working on these skills now will drastically help you when you start interviewing for your next job.

This is especially true if you’re applying for any of the larger tech companies that require you to strongly understand algorithms and data structures.

Properties of a sorting algorithm

In addition to the time and space complexity of sorting algorithms, the below properties help define sorting algorithms.

Adaptability: An adaptive sort’s performance improves the more sorted the list is initially

In-place: An in-place sort requires a constant amount of additional space

Parallelism: A parallel sort can split its workload between multiple workers

Stability: A stable sort preserves the order of items in the list that evaluate to the same value

Algorithmic Paradigms

Divide-and-conquer

This is a common algorithmic pattern based on recursion, which is implemented in both merge sort and quick sort. It breaks one main problem into many sub-problems that are similar to the original problem. It then recursively solves the sub-problems, and finally combines the solutions at the end. Remember, divide-and-conquer can have many more than two subproblems which means we’re really utilizing recursion. If you don’t yet feel comfortable with recursion, I highly recommend this article by Anton Arboleda.

There are three parts to a divide and conquer solution:

  1. Divide the problem into many subproblems that are just smaller instances of the original problem.
  2. Conquer the subproblems by solving them recursively.
  3. Combine the solutions to all subproblems into one solution for the original problem.

Brute Force

This is a pretty straightforward approach to problem solving, and one I’m sure you’re familiar with. It’s considered one of the easiest to apply to small problems. As you might have guessed, this is not normally the most efficient approach that we want to take with our algorithms.

Some examples of brute force sort algorithms include bubble sort and selection sort.

I founded Higglo Digital and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a beautifully designed website, check us out.

I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!

Sorting algorithms to know

If you’re a beginner to sorting algorithms, I highly suggest you start with bubble sort.

--

--