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