Getting Started With Algorithms in JavaScript

Let’s get started…..

Algorithms are step by step solution for a program. So as developer or programmer we all have to learn some some algorithms or write new algorithms.

Let’s learn some popular and useful algorithms.

1.Factorial:

Factorial of a number is multiplication of 1 to number. Factorial of a number is denoted by n!(where n is the number).

n!=1*2*3*4*……..*n.

let’s write a solution in JavaScript.

above solution is called iterative solution. An alternative solution is called recursive solution. let’s see that

In above approach a function call itself again and again which is called recursion.

Yo can use either first one or second one.

2. Fibonacci Number or Series:

A Fibonacci number is the total of it’s two previous number in Fibonacci series.

Fibonacci series be like- 0,1,1,2,3,5,8,13,21,34………..

Let’s write a program to find nth number in series

In recursive approach

3. Linear Search:

Suppose you are given a list or an array of items. You are searching for a particular item. JavaScript code be like that

4. Binary Search:

Binary Search is searching technique which works on Divide and Conquer approach. It used to search any element in a sorted array. Let’s write a solution in JavaScript

5. Bubble Sort:

Bubble sort is a sorting algorithm,which runs on complexity O(n²). which is not an ideal solution for large list.

The idea behind bubble sort is that every pair of adjacent values is compared, and then the two values swap positions if the first value is greater than the second. This way during each pass through the array, the largest value “bubbles up” to the top, and after each pass the elements furthest to the right are in the correct order.

let’s see the code

6. Selection Sort:

Selection sort is a sorting algorithm runs on complexity O(n²) time. This algorithm is very simple and easy to implement, however it is also very inefficient .

let’s see an example

7. Insertion Sort:

Before we dive into the specifics regarding the implementation of insertion sort, let’s talk more about it’s benefits. Insertion sort runs in O(n²), or quadratic, time in the worst case. This typically isn’t very effective and should not be used for large lists. Because of insertion sort’s low hidden constant value, however, it usually outperforms more advanced algorithms such as quick sort or merge sort on smaller lists. Some implementations of those advanced algorithms will even switch from using their more advanced methodology to insertion sort when the list gets small enough. In practice, insertion sort is also usually more efficient than other quadratic sorting algorithms such as bubble sort or selection sort. It’s best case run time is O(n), or linear, which occurs if the input array is already sorted. On average, insertion sort’s run time is still quadratic.

8. Quick Sort :

If you’re interviewing for Software Engineering position, one of the more intimidating questions to deal with is explaining how the Quicksort algorithm works.

The simplest algorithmic steps for Quicksort is:

1. Pick a pivot element that divides the list into two sublists. We can select a random element as the pivot.
2. Reorder the list so that all elements less than the pivot element are placed before (towards its left) the pivot and all elements greater than the pivot are placed after it (towards its right).
3. Repeat steps 1 and 2 on both the smaller and larger list. That is, Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

Below is a basic implementation without usuing swap function and partition function.

9. Merge Sort:

Merge sort is an example of a divide-and-conquer type sorting-algorithm. The input for merge sort is an array of integers of length n, which needs to be sorted, typically from least to greatest. What merge sort does is it splits the unsorted array into two parts and then you recursively apply merge sort to these sub-arrays to further split the arrays until you are left with a bunch of single-element arrays. Then, you compare single-element arrays to one another before recombining them into a two-element, sorted array (and so on). If you do this repeatedly, eventually you end up with a single, sorted array of length n. Sorting algorithms are surprisingly complicated to grasp at first and I’m still trying to wrap my mind around the intricacies of computer sorting in general, but here is my attempt at implementing merge sort in JavaScript:

part 1:

part 2:

That’s it ! wish me luck !

A full-stack Web and Mobile Apps developer.

More from Md Kawsar

A full-stack Web and Mobile Apps developer.