JS array search-Implementation of half-fold search

Source: Internet
Author: User
This article mainly introduces how to implement the binary search of javascript Array search, and analyzes the principles, implementation skills, and related Precautions of the Binary Search Algorithm of javascript Array in combination with examples, if you need it, refer to the next article to introduce how to implement a half-fold search for JS array search, the principles, implementation skills, and precautions of the javascript array semi-half search algorithm are analyzed based on the specific examples. For more information, see

This article describes how to implement a half-fold search for JS array search. We will share this with you for your reference. The details are as follows:

I. method principle:

When you look for a specific value from a given sequence array arr, the semi-query method does this:

1. Determine the start point of the search range: Start startIndex = 0, end endIndex = arr. length-1;

2. determine an intermediate point middle = Math. floor (end-start point)/2) based on the start point );

3. Compare the size of arr [middle] and value on the premise of startIndex <endIndex:

(1) arr [middle] <value

Adjust the search range to the second half of the array, that is, startIndex = middle + 1, endIndex = arr. length-1;

(2) arr [middle]> value

Adjust the search range to the first half of the array, that is, startIndex = 0, endIndex = middle-1;

Then, recalculate the middle and compare the values of arr [middle] With the value until the two are equal or startIndex> = endIndex.

Ii. Code:

// This example is applicable to the function binarySearch (arr, value) {var startIndex = 0, endIndex = arr. length-1; middle = Math. floor (endIndex-startIndex)/2); while (arr [middle]! = Value & startIndex <endIndex) {if (arr [middle]> value) {endIndex = middle-1;} else if (arr [middle] <value) {startIndex = middle + 1;} middle = Math. floor (endIndex-startIndex)/2);} return (arr [middle]! = Value )? -1: middle ;}

Iii. Advantages and Disadvantages:

(1) Advantages:

Each time a query is performed, the number of array items to be searched is halved. Therefore, the search performance is better than the linear search method (especially when there are many array items );

(2) Disadvantages:

This method is only applicable to sequence arrays. before using this method for common arrays, you must sort the arrays.

The above is a detailed description of the implementation method of JS array search, please pay attention to other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.