Important Topic of Algorithm

BH Nibir
4 min readMay 9, 2020

--

1.Algorithm

Algorithmic thinking is a way of getting to a solution through the clear definition of the steps needed.

2. Factorial Using Js

The factorial function is a mathematical formula. In the Factorial formula, you must multiply all the integers and positives that exist between the number that appears in the formula and the number 1.

//5! = 1*2*3*4*5 = 120let num = 5;
temp = 1;
for( let i = 1; i <= num; i++ ) {
temp*=i
}
console.log(temp) // output 120

3. Recursion

When a function that calls itself directly or indirectly. And, this technique is known as recursion. Recursion is used to solve various mathematical problems by dividing it into smaller problems.

function recursionFunc(num) {
return num > 1 ? num * recursionFunc( num - 1 ) : 1;
}
console.log(recursionFunc(5))

4.Fibonacci Number

The Fibonacci Sequence is the series of numbers like : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

The next number is found by adding up the two numbers before it.


const len = 8;
let num1 = 0;
let num2 = 1;
let result = num2;
for( let i = 1; i <= len ; i++ ) {
console.log(result)
result = num1+num2
num1 = num2
num2 = result
}
//1 1 2 3 5 8 13 21

5.Linear Search

Linear search is a very simple search Algorithm. In this type of search, a sequential search is made over all items one by one.

Github
const arr = [ 1, 3, 2, 4, 5, 6, 8, 9, 7 ]function linearSearch(arr, key){
for(let i = 0; i < arr.length; i++){
if(arr[i] === key){
return i
}
}
return -1
}console.log(linearSearch(arr, 5)) //Output: 4 Item Index

6.Binary Search

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you’ve narrowed down the possible locations to just one.

const arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]function binarySearch(array, target){
let start = 0;
let end = array.length - 1;
while(start <= end) {
let mid = Math.floor((start + end) / 2);
if(target === array[mid]) {
return mid
}
else if(target > array[mid]) {
start = mid + 1;
}
else{
end = mid - 1;
}
}
return -1
}console.log(binarySearch(arr, 7)) // index 6
console.log(binarySearch(arr, 3)) // index 2
console.log(binarySearch(arr, 11)) // index -1

7.Bubble Sort

Bubble Sort is a simple sorting algorithm that repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

8.Selection Sort

Selection Sort algorithm repeatedly searches remaining items to find the least one and moves it to its final location.

9.Insertion Sort

Insertion sort compares the current element with the next element and determines whether the current element is greater than the one it was compared to.

If this is true, then it leaves the element in its place and moves on to the next element. If it is false, then it finds its correct position in the sorted array and moves it to that position by shifting all the elements which are larger in the sorted array to one position ahead.

10. Complexity

Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n).

--

--