Home > Developer > JavaScript

Source: Internet
Author: User

There are 2 ways to find data, sequential lookup and binary lookup. Sequential lookups apply to lists of elements that are randomly arranged. The binary lookup applies to the sorted list of elements. The binary lookup is more efficient, but it must be a collection of list elements that are already sorted.

**One: Sequential lookup**an order lookup is a list element that is judged individually from the first element of the list until the desired result is found, or the element you are looking for is not found at the end of the list.

The code is as follows:

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

We can also return an order lookup function that matches the position of the element, as follows:

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

**Two: Find the minimum and maximum values**

Find the minimum algorithm in the array as follows:

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

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

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

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

5. When the program ends, 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) { min = arr[i]; } } return min; }

Find the maximum algorithm, similar to the minimum above, first set the first element in the array to the maximum, and then iterate over each element remaining in the arrays against the current maximum value, and assign the value of the current element to the maximum value if the value 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) { max = arr[i]; } } return max; }

**Third: The two-point 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 the binary search algorithm is as follows:

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

2. Sets the position of the last element of the array to the upper boundary (the length of the array is reduced by 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 midpoint element is less than the value of the query, the bottom boundary is set to the subscript plus 1 of the midpoint element.

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

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

The code is as follows:

//two-point lookup algorithm function Binsearch (Data,arr) {var lowerbound = 0; var upperbound = arr.length-1; while (lowerbound <= upperbound) {var mid = Math.floor ((upperbound + lowerbound)/2); if (Arr[mid] < data) {Lowerbound = mid + 1; }else if (Arr[mid] > data) {upperbound = mid-1; }else {return mid; }} return-1; }//Quick sort function qsort (list) {if (list.length = 0) {return []; //Store value less than datum 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 (list[i)); }else {Right.push (List[i])}} return Qsort (left) concat (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: Calculate the number of repetitions;**when the binary lookup algorithm Binsearch () function finds a value, if other values in the dataset appear, the function is positioned near a similar value, in other words, the other same values may appear to the left or right of the found value.

So our simplest scenario is to write 2 loops, one at a time to traverse the dataset 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:

Calculates the repeat Count function count (data,arr) { var count = 0; var arrs = []; var position = Binsearch (Data,arr); if (Position >-1) { ++count; Arrs.push ({"Index": Count}); for (var i = position-1 i > 0;-i) { if (arr[i] = data) { ++count; Arrs.push ({"Index": Count}); } else {break ; } } for (var i = position + 1; i < arr.length; ++i) { if (arr[i] = data) { ++count; Arrs.push ({"Index": Count}); } else {break ; } }} return arrs; } Test repeat number of 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);

As shown in the following illustration:

Related Article