Javascript data structure and algorithm retrieval algorithm, javascript Data Structure

Source: Internet
Author: User

Javascript data structure and algorithm retrieval algorithm, javascript Data Structure

There are two ways to query data: sequential search and binary search. Sequential search lists suitable for random arrangement of elements. Binary Search applies to the list of sorted elements. Binary Search is more efficient, but it must be a list element set with sorted order.

I. Sequential search
Sequential search starts to judge the list elements one by one from the first element of the List until the desired result is found or until the end of the list is not found.

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 the sequential lookup function for Matching Element positions. 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;}

Ii. Search for the minimum and maximum values

The algorithm for finding the minimum value in an array is as follows:

1. Assign the first element of the array to a variable and use this variable as the minimum value.
2. Start to traverse the array and compare the second element with the current minimum value.
3. If the value of the current element is smaller than the current minimum value, the current element is set to the new minimum value.
4. Move to the next element and 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;}

The algorithm for finding the maximum value is similar to the above minimum value. First, set the first element in the array to the maximum value, and then compare the remaining elements of the array with the current maximum value cyclically, if the value of the current element is greater than the current maximum value, the value of this element is assigned to the 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; }

Iii. Binary Search.

If the data you want to search for is ordered, the binary search algorithm is more efficient than the sequential search algorithm. The basic principle of the 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 bottom boundary is equal to or less than the upper boundary, perform the following operations:
A. Set the midpoint to (upper boundary plus lower boundary) divided by 2.
B. If the element of the midpoint is smaller than the queried value, set the lower boundary to the subscript where the midpoint element is located and Add 1.
C. If the midpoint element is greater than the queried value, set the upper boundary to the subscript where the midpoint element is located minus 1.
D. Otherwise, the midpoint element is the data to be searched and can be returned.

The Code is as follows:

// Binary Search 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 the value smaller than the reference value var left = []; // store the value greater than the reference value var right = []; var parts = list [0]; for (var I = 1; I <list. length; I ++) {if (list [I] <distinct) {left. push (list [I]);} else {right. push (list [I])} return qSort (left ). concat (weights, qSort (right);} // test code var numbers = [,]; var list = qSort (numbers); console. log (binSearch (6, list ));

4. Calculate the number of duplicates;
When the binSearch () function of the Binary Search Algorithm finds a value, if other identical values exist in the dataset, the function locates near similar values. In other words, if other values are the same, the Left or Right side of the value may appear.

The simplest solution is to write two cycles. One is to traverse the dataset downward or left at the same time to count the number of repetitions. Then, we traverse the dataset up or right to count the number of repetitions. The Code is as follows:

// Calculate the number of repetitions 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;} // code var arr =, 4, 5, 6, 7, 8, 9]; var arrs = count (1, arr); console. log (arrs); console. log (arrs. length );

As shown in:

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.