Searching and Sorting Algorithms in Javascript — Part 1 (Intro)
For the most part, sorting and searching are some of the very frequent things you encounter in your daily tasks as a software engineer (or even in your day-to-day life). In your daily life activities, you have to sort your tasks based on priority, “search” for where you dropped your car keys, where you dropped that phone of yours, and so on…
In your work as a software engineer, you might encounter the task of sorting an array of numbers or searching through a list of inventory, and some other complex searches that you might imagine.
Now, it begs the question, what does an algorithm have to do with searching and sorting? To answer this question, let’s ask Wikipedia to tell us what an algorithm is (me: “opens a tab on my browser and starts searching”). According to Wikipedia:
an algorithm is a self-contained step-by-step set of operations to be performed.
In lame man terms, an algorithm is just a step-by-step instruction on how to perform a task or how to solve a particular problem. With that defined, we can say that a sorting algorithm is a step-by-step instruction on how to arrange the items in a given list while a searching algorithm on the other hand is the step-by-step instructions on how to locate an item in a particular list.
There are several different sorting algorithms in computer science. To name a few, we have:
- Bubble Sort Algorithm
- Selection Sort Algorithm
- Insertion Sort Algorithm
- Merge Sort Algorithm
- Quick Sort Algorithm
- Radix Sort Algorithm
For searching algorithms, we have (just to name a few):
- Linear Search
- Binary Search
- Interpolation Search
- Exponential Search
… And so on. There are other more advanced, searching and sorting algorithms that were not mentioned here because of how complex they are. The ones mentioned here are just some of the basic ones, and I’ll do well to explain some of them as much as I can.
Why do we need so many algorithms to perform the same task??!!😖
To answer that question, we need to look at something called the Big O Notation. The Big O Notation is a way of measuring the efficiency of an algorithm.
There are 2 parts to measuring efficiency — We have the time complexity and the space complexity of an algorithm. Time complexity refers to HOW FAST an algorithm does what it does in the worst case scenario while Space Complexity refers to HOW MUCH SPACE (MEMORY) the algorithm uses while performing its operations in the worst case scenario.
These 2 measures (time and space complexity) are the major things that differentiate these algorithms from one another. Other things to consider are the ease of implementation of the algorithm, the size of the list you’re working with, and the relationship between the items in the list.
In the next series of posts, I’ll pick each of the algorithms listed here one after the other (starting from sorting algorithms) and explain them further, including how to implement them in Javascript. Although Javascript provides some helper functions to perform some of these operations (searching and sorting), it helps to know how they work and how to implement them (especially if you’re preparing for a technical interview).
See you on the next one!