A search algorithm of JavaScript data structure and algorithm

Source: Internet
Author: User
Tags min

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:

1 2 3 4 5 6 7 8 function Seqsearch (Data,arr) {for (var i = 0; i < arr.length; ++i) {if (arr[i] = = data) {return true;}} ; }

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

1 2 3 4 5 6 7 8 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:

1 2 3 4 5 6 7 8 9 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:

1 2 3 4 5 6 7 8 9 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The "///two-point lookup algorithm function, binsear CH (data,arr) {var lowerbound = 0; var upperbound = arr.length-1; while (lowerbound <=-Upperbound) {var mid = Math.fl Oor ((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 of less than datum var left = [];//store value greater than 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 (Pivot,qsort); ///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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Calculate 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.