Sequential lookup JavaScript

Source: Internet
Author: User

The simplest way to find data is to start by judging the list elements from the first element of the list until you find the results you want. This method is called sequential lookup and is sometimes called linear lookup. It is one of the techniques of finding violence.

Sequential lookups are very simple to implement, with the following code:

function Generalsearch (arr,data) {//normal sequential lookup, which is to iterate over to see if a for (Var i=0;i<arr.length;i++) {if (arr[i]==data) {return true;}} return false;}

So will it be inefficient? For unsorted datasets, finding is the fastest and most successful when the data being found is in the starting position of the dataset. By placing successfully found elements at the beginning of the dataset, you can guarantee that elements will be found faster in future operations, with the following code:

function Bettersearch (arr,data) {//self-organizing lookup, move the lookup rate forward in turn for (Var i=0;i<arr.length;i++) {if (arr[i]==data) {if (i>0) { Swap (arr,i,i-1);//If found, the value to be looked up and the previous value Exchange position}return true;}} return false;} Function Swap (ARR,I,J) {//Swap position temp=arr[i];arr[i]=arr[j];arr[j]=temp;}

Is there any better way to do that? In the world of lookups, there is a "80-20 principle", which refers to a 80% lookup operation performed on a data set that finds 20% of the data elements. So we can put the found and the last 80% elements in the starting position, and the first 20% does not need to change, the code is as follows:

function Bestsearch (arr,data) {///better self-organizing lookup, the result of the post-ranking 80% lookup to the first for (Var i=0;i<arr.length;i++) {if (arr[i]==data& &i> (arr.length*0.2)) {//If it is 80%swap (arr,i,0); return true;} else if (arr[i]==data) {return true;//first 20% does not move}}return false;}

The experiment code for three lookups is as follows:


Test Var nums=[3,1,4,6,2,9,8,0,5,7];//General find Var bool=generalsearch (nums,3);d ocument.write (bool+ ' <br> ');// Truevar Bool=generalsearch (nums,11);d ocument.write (bool+ ' <br> ');//false//self-organizing find shownums (nums);//3 1 4 6 2 9 8 0 5 7betterSearch (nums,2); Shownums (nums);//3 1 4 2 6 9 8 0 5 7betterSearch (nums,2); Shownums (nums);//3 1 2 4 6 9 8 0 5 7betterS Earch (nums,2); Shownums (nums);//3 2 1 4 6 9 8 0 5 7//Better self-organizing find document.write ("Better self-organizing find <br>"); Bestsearch (nums,5); Shownums (nums);//5 2 1 4 6 9 8 0 3 7bestSearch (nums,2); Shownums (nums);//5 2 1 4 6 9 8 0 3 7

Complete code for sequential lookups:



Sequential lookup JavaScript

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.