Algorithms in JavaScript

Marow Macaulay
Backend Developers
Published in
5 min readAug 6, 2020

The word “Algorithms” or “Algo” would impact fear in anyone who isn’t really strong in maths like me. Well today i’m not here to impact fear into anyone, and i’m not also here to dispute the fact that Algorithms isn’t hard. Today i am going to discuss easier ways of understanding some sorting methods in Algorithms which includes: “INSERTION SORT METHOD” and the “MERGE SORT METHOD” a.k.a “DIVIDE AND CONQUER METHOD”.

OUTLINE

What is an Algorithm?

Why are Algorithms even important?

INSERTION and MERGE SORT algorithm methods in JavaScript.

PREREQUISITE

A basic understanding of JavaScript data structures, and Iterating methods.

A fundamental knowledge of Algorithms and Data structures.

Definitely a Programming experience :)

What is an Algorithm?

Algorithmsalgorithms algorithms…

Simply put, a well-defined computational-procedure that involves taking a set of input as values, and producing a set of values as output is known as an Algorithm. Let’s break it even further, a defined procedure/process that involves taking an input and giving out an output is an algorithm.

Algorithms has now evolved more than being a procedure that takes an input and gives out an output, it is now more recognized as a tool for solving well-defined computational problems.

Understanding algorithms is a skill every human being, software professional could have but might not know. Let me explain quite further, the Human Brain works in ways we might not even understand, but when it comes to selecting the best out of the worse things, there you go… Your Brain creates it’s own Algorithm.

To illustration: Suppose you went to shop for some oranges and on getting home you discovered that some were bad, well your brain does the sorting process and then you filter the shopping bag to contain only the good oranges left and probably return the bad ones.

WHY ARE ALGORITHMS IMPORTANT?

Algorithms!!! Why are they even important?

Algorithms are very important because the world works on Algorithms. You might be amazed at that, but it’s the fact. Almost everything in the real world involve algorithms just in-case you didn’t know. Computers, Machines, Applications, Languages were all built on Algorithms. So knowing how Algorithms work doesn’t only make you a better software developer, but it also allows you to understand how the world really works.

INSERTION SORT METHOD

At last, our first algorithm method. The INSERTION method is one of the methods in algorithm sorting methods. Let us take note of the keyword sorting, we do this everyday but we might not even realize that there are better ways of implementing the sort method. Well in Algorithm there is a way to identify if a particular method is quite efficient. But how? We measure the Speed(secs) it takes for a specific method to run as the number of inputs grows.

The Insertion sort method is a very familiar way of sorting things in the real world. Suppose we have a deck of shuffled cards spread across a table with the numbers on the card showed to us, the way we sort the cards from the lowest number to the highest number, is very similar to the insertion sort method. You start by putting the card with the lowest number in the first set, if only the next card in the set is greater than the last card, based on the number on it.

Let me quickly explain this using a code sample. The code sample will be written in JavaScript, but do well to note that Algorithms can be implemented in any Programming Language of your choice.

The preceding code sample shows a step-by-step process on the INSERTION sort method works. The Data Structure used here is an Array. An Array is a special kind of data structure that is used to store values as elements, and the elements are accessed by it’s index.

In the line 1 of the code, we initialized an Array that contained unordered set of numbers. Then we used a for-loop(2) to iterate through the elements in the Array by it’s index and length property and then check if (5)the second element in the array is less than the first element, if the condition is true we swap the elements in the array to fit in an ascending order, but if the condition is false, we set the element to be in it’s original position(9).

If you have to change the format to a descending order, you could try that by changing the condition symbol in line 5 to (<). That is:

5   while(j > -1 && numbers[j] < key) //Descending order

So there we have it, an algorithm method that is used to sort a given set of inputs in an Ascending order. Pretty clear and straight-forward. This algorithm is quite efficient. Although i will discuss ways in which we can know if a specific algorithm method is efficient in another Article. But for the time-being, you can keep it in mind that the “MERGE SORT” is more efficient than the INSERTION SORT as the number of inputs in an “Array grows” or increases.

MERGE SORT METHOD (DIVIDE AND CONQUER)

Oh yeah!! We made it to the final method for today. The MERGE METHOD is one of the best methods i have ever seen in SORT ALGORITHMS. This method even has it’s own principle attached to it, just to show how unique and interesting it is. The “DIVIDE AND CONQUERprinciple.

This principle is quite easy to understand, because just as the name implies, you DIVIDE the PROBLEM into smaller bits, then SOLVE those smaller bits into reliable SOLUTIONS, after that SUM all those bits as one, and you have CONQUERED the problem.

To illustrate, let us consider the following:

//An array of 14 elements
let numbers = [10,5,4,2,9,6,8,7,11,14,13,1,12,3];
//Divide this array into smaller bits
[10,5,4,2,9,6,8] [7,11,14,13,1,12,3]
[10,5,4] [2,9,6] [8] [7,11,14] [13,1,12] [3]//keep dividing
[10,5] [4] [2,9] [6] [8] [7,11] [14] [13,1] [12] [3] //...
//Keep dividing to it's smallest parts
[10] [5] [4] [2] [9] [6] [8] [7] [11] [14] [13] [1] [12] [3]
//Then sorts the smallest parts
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
//And Sum it up
[1,2,3,4,5,6,7,8,9,10,11,12,13,14]//=> Finally Conquered it...

Let us quickly see a code sample that explains how this all works:

CONCLUSION

The MERGE METHOD is also another useful way of implementing the sort Algorithm and it also seems to be more efficient than the INSERTION method. Let me not bug you with efficiency for now.

Later on , i will publish an Article on “How to get the efficiency of an Algorithm and why we need to know how efficient an Algorithm method is”. Until then, i hope i have been able to enlighten you my distinguished audience.

Thank you for reading my article. Here at my blog or medium I regularly write about backend development, digital marketing and content management system. To read my future posts simply join my publication or click ‘Follow’ Also feel free to connect with me via Twitter, Facebook, Instagram.

If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my Youtube channel, we will be posting a collection of help full tutorials and guides like this one for artisans.

If you enjoy this post, make sure to let us know and share it with your friends and subscribe to my growing channel.

Sharing is caring.

--

--