Learning Algorithms in JavaScript

Hridoy Banik
3 min readMay 9, 2020

In this article, we will discuss some searching and sorting Algorithm those are implemented in JavaScript.If you are in the middle of learning JavaScript,you can read it.

Linear Search Algorithm

Assume that, there is a bookshelf and we have to find a book from that shelf.If you search randomly, then there is a possibility not to find out that particular book.If we go for finding that book one by one instead of randomness,there is possibility to find out that books.If not,then we clearly claim that the book is not here.In computer science,that one by one searching process is called linear.We can assume the bookshelf as a array and every element of that bookshelf if is book. Linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.The time complexity is O(1)

collected from google

Implementation is given below:

Binary Search Algorithm:

Binary search algorithm works in sorted array.If the element is in the sorted array,it returns the index of that value.The target element is compare with the middle element of the array.And repeatedly eliminate the half search interval at a time.If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

photo from google

Implementation of binary search algorithm:

Bubble Sort Algorithm:

Bubble Sort is a sorting method where each element are comparing to the element behind it. So, if you have an array with [6,5,3,1,8] the bubble sort function would compare "6" to "5" then compare "5" to "3" and so on until the array is sorted.

Implementation of bubble sort:

Selection Sort Algorithm:

A selection sort runs through an array once for each element. In each iteration, it finds the smallest value in the set of elements starting from that element through to the end of the array. At the end of that run, if that element isn’t the smallest, it swaps it with the one that is.

Implementation of Selection Sort:

Insertion Sort Algorithm:

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

implementation of Insertion sort:

good bye!!

--

--