JavaScript data structure and algorithm---retrieval algorithm

Source: Internet
Author: User

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.

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.

The code is as follows:

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

We can also return the search function in the order that matches the position of the element, the code is as follows:

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.

The code is as follows:

function Findmin (arr) {    var min = arr[0];      for (var i = 1; i < arr.length; + +i)        {if(Arr[i] < 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:

function Findmax (arr) {    var max = arr[0];      for (var i = 1; i < arr.length; + +i)        {if(Arr[i] > 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.

The code is as follows:

//binary search AlgorithmfunctionBinsearch (Data,arr) {varlowerbound = 0; varUpperbound = arr.length-1;  while(Lowerbound <=upperbound) {        varMid = Math.floor ((upperbound + lowerbound)/2);if(Arr[mid] <data) {Lowerbound= Mid + 1; }Else if(Arr[mid] >data) {Upperbound= Mid-1; }Else {            returnmid; }    }    return-1;} //Quick SortfunctionQSort (list) {if(List.length = = 0) {        return []; }    //store values that are less than the base value    varleft = []; //store values greater than the base value    varright = []; varPivot = list[0];  for(vari = 1; i < list.length; i++) {        if(List[i] <pivot)        {Left.push (list[i]); }Else{Right.push (List[i])}}returnQSort (left). Concat (Pivot,qsort);} //Test Codevarnumbers = [0,9,1,8,7,6,2,3,5,4];varList =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:

//count Repetitionsfunctioncount (Data,arr) {varCount = 0; varArrs = []; varPosition =Binsearch (Data,arr); if(Position >-1) {        ++count; Arrs.push ({"Index": Count});  for(vari = position-1; i > 0; --i) {if(Arr[i] = =data) {                ++count; Arrs.push ({"Index": Count}); }Else {                 Break; }        }         for(vari = position + 1; i < arr.length; ++i) {if(Arr[i] = =data) {                ++count; Arrs.push ({"Index": Count}); }Else {                 Break; }        }    }    returnArrs;} //code to test the number of repetitionsvararr = [0,1,1,1,2,3,4,5,6,7,8,9];varArrs = count (1, arr); Console.log (Arrs); Console.log (arrs.length) ;

As shown in the following:

JavaScript data structure and algorithm---retrieval algorithm

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.