DEV Community

Ashutosh Sarangi
Ashutosh Sarangi

Posted on

2 1 1 1 1

Searching Algorithm Using Javascript

There are 2 types of searching algorithms are there as below.

  1. Linear Search
  2. Binary Search (We must get Sorted array as an input)
const linearSearch = (arr, value) => {
    for (let item of arr) {
        if (item == value) {
            return true;
        }
    }
    return false;
};

console.log(linearSearch([1,2,3,4,5,6,7,8,9], 7));
console.log(linearSearch([1,2,3,4,5,6,7,8,9], 3));
console.log(linearSearch([1,2,3,4,5,6,7,8,9], 10));
console.log(linearSearch([1,2,3,4,5,6,7,8,9], 9));

Enter fullscreen mode Exit fullscreen mode

Binary Search


const binarySearch =  (arr, value, low = 0, high=arr.length-1) => {
    const mid = Math.floor((low + high)/2);
    const midValue = arr[mid];
    if (low > high) {
        return false;
    }
    if (midValue == value) {
        return true;
    } else if (midValue > value) {
        return binarySearch(arr, value, low, mid);
    } else {
        return binarySearch(arr, value, mid+1, high);
    }
}

const arr = [9,8,7,6,5,4,3,2,1];
arr.sort((a,b) => a-b);

console.log(binarySearch(arr, 7));
console.log(binarySearch(arr, 3));
console.log(binarySearch(arr, 10));
console.log(binarySearch(arr, 9));

Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay