JavaScript code Implementation of the 80-20 principle of data lookup

Source: Internet
Author: User

As a front-end developer, whether at work or looking for a job (written test/interview), more or less involves some data structure knowledge.

A data structure is how your computer stores and organizes your data.

Common structures: arrays, stacks, queues, linked lists, trees, graphs, heaps, and hashes

With regard to data, we often use operations such as sorting, finding (retrieving), and so on. The usual sorts are bubble sort, select sort, insert sort, hill sort, merge sort, and quick sort. Common search methods are: Sequential lookup and Binary lookup. In order to find, 80/20 rules are beneficial to the organization and management of data, and improve the efficiency of data searching.

The premise of the 80/20 principle is that the location of the data to be found is in the latter 80% of the array, and its driving force is the number of data lookups. That is, when a number is located in the last 80% parts of an array, each time it is looked up, the data is shifted forward one bit. The JavaScript implementation code for the 80/20 principle is as follows:

<! DOCTYPE html>//Swap Functions        functionSwap (ARR,A1,A2) {vartemp =ARR[A1]; ARR[A1]=ARR[A2]; ARR[A2]=temp; }    //Main code Logic        functionSquserch (arr,data) {varn =arr.length;  for(vari=0;i<n;i++){                if(Arr[i]==data&&i> (n*0.2) {swap (arr,i, (i-1)); }            }            return-1; }    //Example Demo:        varnum=[23,4,7,8,5,2,9]; Squserch (Num,5); Squserch (Num,5); Squserch (Num,5); Squserch (Num,5);    Console.log (Num); </script></body>

Operation Result:

It is known that with the increase in the number of data "5" lookups, the position is gradually shifted to the left: from the original 5th position to the 2nd position.

JavaScript code Implementation of the 80-20 principle of data lookup

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.