Home > Web Front-end > JS Tutorial > body text

Searching Algorithm Using Javascript

王林
Release: 2024-08-18 00:03:32
Original
1143 people have browsed it

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));

Copy after login

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));

Copy after login

The above is the detailed content of Searching Algorithm Using Javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!