JavaScript Search Algorithm code example

Source: Internet
Author: User

I saw an article about JavaScript today. To arouse my curiosity. There are 2 ways to find data, sequential lookups and two-point lookups. The sequential lookup applies to the list of elements that are randomly arranged. The binary lookup applies to the list of elements that have been sorted. Binary lookups are more efficient, but must be a set of list elements that are already ordered.

In the more general talk of today. This JavaScript article is a rarity. The main interview in this codego.net JavaScript article is the search algorithm. Includes some demos of data structures and algorithms.

Personally feel very useful. So I don't dare to give it to you alone. Plus some of my own experience.

One: Sequential lookup

A sequential lookup is a list element that is judged one at a start from the first element of the list until the desired result is found, or until the end of the list does not find the element you want to find.

function Seqsearch (Data,arr) {for (var i = 0; i < arr.length; ++i) {if (arr[i] = = data) {return true; }} return false; }

We can also return the order of matching element positions to find the function

function Seqsearch (Data,arr) {for (var i = 0; i < arr.length; ++i) {if (arr[i] = = data) {return i; }} return-1; }

Two: Find minimum and maximum values

Find the minimum value algorithm in the array as follows:

1. Assign the first element of the array to a variable, and use the variable as the minimum value.

2. Begin iterating through the array, comparing the second element to the current minimum value in turn.

3. If the value of the current element is less than the current minimum, the current element is set to the new minimum value.

4. Move to the next element and repeat step 3.

5. At the end of the program, the minimum value is stored in this variable.

function Findmin (arr) {var min = arr[0];     for (var i = 1; i < arr.length; ++i) {if (Arr[i] < min) {min = arr[i]; }} return min; }

The Find Max algorithm is similar to the above minimum value, first sets the first element in the array to the maximum value, and then loops each remaining element of the arrays against the current maximum value, and assigns the value of the element to the maximum value if the value of the current element is greater than the current maximum value. The code is as follows:

? 123456789 function Findmax (arr) {var max = arr[0];     for (var i = 1; i < arr.length; ++i) {if (Arr[i] > max) {max = arr[i];  }} return max; }

Three: Two-part search method.

If the data you are looking for is ordered, the binary lookup algorithm is more efficient than the sequential lookup algorithm. The basic principle of binary search algorithm is as follows:

1. Set the first position of the array to the lower boundary (0).

2. Set the position of the last element of the array to the upper boundary (the length of the array minus 1).

3. If the lower boundary is equal to or less than the upper boundary, do the following:

A. Set the midpoint to (upper boundary plus bottom boundary) divided by 2.

B. If the element in the midpoint is less than the value of the query, set the lower boundary to the subscript of the midpoint element plus 1.

C. If the midpoint element is greater than the value of the query, set the upper boundary to the midpoint element where the subscript minus 1.

D. Otherwise the midpoint element is the data to be found and can be returned.

Binary Lookup algorithm function Binsearch (Data,arr) {var lowerbound = 0;   var upperbound = arr.length-1;   while (Lowerbou nd <= upperbound) {    var mid = Math.floor ((upperbound + lowerbound)/2)     if (Arr[mid] < dat A) {      lowerbound = mid + 1,    }else if (Arr[mid] > data) {      Upperbou nd = mid-1;    }else {      return mid,    }  }   return-1; }  //Quick Sort function QSort (list) {  if (list.length = = 0) {    return [];  }  //store values less than the base value   var left = [];  //Store values greater than the base value   var right = [];   var pivot = list[0];   for (var i = 1; i < list.length; i++) {    if (List[i] < pivot) {      Left.push (LIS T[i]);    }else {      Right.push (List[i])    }  }   return QSort (left). Concat (P Ivot,qsort (right)); }  //Test code var numbers = [0, 9,1,8,7,6,2,3,5,4]; var list = QSort (numbers); Console.log (Binsearch (6,list)); 

Four: Count the number of repetitions;

When the binary lookup algorithm Binsearch () function finds a value, if there are other identical values in the dataset, the function is positioned near similar values, in other words, the same values may appear to the left or right of the found value.

Then our simplest solution is to write 2 loops, one for the data set to traverse down or to the left, to count the number of repetitions, and then to traverse up or to the right to count the number of repetitions. The code is as follows:

//Computes the number of repetitions function count (Data,arr) {  var count = 0;   var arrs = [];   var position = Binsearch (data,a RR);   if (Position >-1) {    ++count,     Arrs.push ({"Index": Count}),     for (var i = P osition-1; i > 0; -i) {      if (arr[i] = = data) {        ++count;         Arrs.pus H ({"Index": Count});      }else {        break,      }    }     for (v Ar i = position + 1; i < arr.length; ++i) {      if (arr[i] = = data) {        ++count,         Arrs.pus H ({"Index": Count});      }else {        break,      }    }  }   RET Urn Arrs; }  //test the number of repetitions of the code var arr = [0,1,1,1,2,3,4,5,6,7,8,9]; var Arrs = count (1,arr); Console.log (ARRS); Console.log (arrs.length);  


This article is from the "Zhentanxiaoxiao" blog, make sure to keep this source http://9888127.blog.51cto.com/9878127/1628187

JavaScript Search Algorithm code example

Related Article

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.