Self-organizing search program

In the homework, it is proposed to call "find" and "find2" with each digit of the next array. However, I am stuck because I do not understand what the code does in the first place. This program, taken from the text, "checks the algorithm for 100 elements with a set of locales that contains 20% of elements and 30% of links."

{9,0,0,6,3,9,2,4,5,9,0,0,6,3,9,2,4,5,9,0,0,6,3,9,2,4,5} //A given set repeated 3 times 

I was told that:

  • I have to change the current bodies for the loop.
  • I have to print the array that was searched (with the usual digits transferred to an earlier position).

What I am asking is an explanation of the following. Then I can continue to manipulate the code.

  • What exactly does find2 do?
  • What happens inside the for loops in which find and find2 are called?
  • How are 4286 and 3903 counters calculated (as seen at the output)? (one of the previous questions may answer this question)

code:

 private static int x[]=new int[100]; public static void main(String[] args){ Random r=new Random(17); //Making the array// for(int i=0;i<x.length;i++)x[i]=x.length-i; System.out.println(Arrays.toString(x)); r.setSeed(17); // straight search for(int i=0;i<x.length*1;i++){ float p=r.nextFloat(); int j=(int)(p*79); if(p<0.3)find(j%20+1); else find(j+21); } System.out.println(n+" "+count); //identical but self-organizing r.setSeed(17);count=0;n=0; for(int i=0;i<x.length*1;i++){ float p=r.nextFloat(); int j=(int)(p*79); if(p<0.3)find2(j%20+1); else find2(j+21); } System.out.println(Arrays.toString(x)); System.out.println(n+" "+count); } //Find private static int find(int target){ for(int i=0;i<x.length;i++) if(x[i]==target){ count+=i;n++; return i; } return -1; } static int count=0,n=0; static final int NEAR=100/10; //Find2 private static int find2(int target){ for (int i=0;i<x.length;i++) if(x[i]==target){ count+=i; n++; if(i>NEAR){ //swap to NEAR x[i]=x[NEAR]; x[NEAR]=target; } else if(i!=0){ //swap with predecessor x[i]=x[i-1]; x[i-1]=target; } return i; } return -1; } 

Output:

 [100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] 100 4286 [99, 100, 97, 96, 98, 92, 93, 91, 94, 95, 5, 84, 56, 46, 86, 52, 83, 87, 49, 82, 3, 78, 45, 53, 44, 75, 74, 6, 2, 71, 85, 69, 18, 7, 19, 68, 64, 81, 62, 12, 88, 16, 4, 57, 90, 61, 55, 70, 63, 51, 50, 73, 48, 47, 1, 89, 79, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 65, 80, 17, 58, 15, 14, 13, 72, 11, 10, 9, 8, 67, 66, 54, 59, 77, 60, 76] 100 3903 

Any help is most appreciated. Also, please let me know if something is wrong with the way I asked this question, since this is my first.

Edit: I tried the following changes and the result was changed. But not understanding what the code does, I definitely don’t know the underlying effects.

 private static int y[]={9,0,0,6,3,9,2,4,5,9,0,0,6,3,9,2,4,5,9,0,0,6,3,9,2,4,5}; //placed right under x[] for(int j=0;j<y.length;j++)find(y[j]); //for the 'straight search' for-loop body for(int j=0;j<y.length;j++)find2(y[j]); //for the 'self-organizing' for-loop body 
+6
source share
1 answer
  • <B> 1. What exactly does 2 find?
    A self-organizing algorithm tries to organize itself in such a way that more frequently used objects are closer to the front. Thus, when searching for an array, most often the search results are more quickly detected during subsequent searches. For example, if you find and find 1 in i = 99, then moving it to NEAR (10) will cause it to be retrieved 10 times a second time. However, from what I saw, the idea of ​​creating a sorted array to show this looks silly. If you already have a sorted array, say 1-100 descending, as in your example, you can just check the target and split the array, or rather give it directly as x [100-target], and that will really give you the target. If it were me, I would randomize the elements, okay.

2. What happens inside the for-loops that find and find2 find in?
It simply generates * pseudo * random numbers as targets. They are pseudo, so the results are the same every time the "random" numbers are not so random. They are also limited by the data set, since nextFloat () will return a number between 0.0 and 1.0 - p * 79, just give a percentage of 79, and then based on p you then calculate the target, which will be between 1 and 100.

3. How are bills 4286 and 3903 received?
These are counts that try to show you that you did what you tried, that is, find the data faster. Basically, a counter is a battery. Each time the target is in i, I am added to the score. If you have three elements found in i = 1, 2 and 3, the score will be 6. The greater the sum of all the indices of the goals found, the less that they will not be organized. Smaller shows that, thanks to the new organization, you were able to improve your search. If performance is not achieved, the numbers will be the same.

You can save the goals in another array and see how often certain goals are called to link what you see as output, at first glance it just looks random.

+5
source

Source: https://habr.com/ru/post/958431/


All Articles