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