I am trying to implement join of two sorted array but the output is all wrong, what should be the modification in the following code?
<p>I am trying union R and S on the basis of R(X,Y,Z) and S(Y,W).If Y attribute for both are same then output the union but the output is wrong. Mostly the problem is in the comparison part but i can't figure it out.   </p>

<pre><code>public int [][]R={{1,2,3},{4,7,8},{2,4,6},{8,9,1},{2,3,4},{3,4,5},{3,4,5,}};
        public int [][]S={{1,2},{4,5},{4,6},{8,9},};

public int[][]M=new int[5][4];
int[][]Output=new int[7][8];
int[][]Disk=new int[10][8];

public void sort(){
    int temp_R[][]=new int[7][3];
    int temp_S[][]=new int[4][2];
    for(int i=0;i&lt;S.length;i++)
    {
        for(int j=i+1;j&lt;S.length;j++)
        {if(S[i][1]&gt;S[j][0])
        {for(int k=0;k&lt;2;k++ )
        {temp_S[i][k]=S[i][k];
         S[i][k]=S[j][k];
         S[j][k]=temp_S[i][k];
        }}
        else
            continue;
        }
    }




    System.out.println("Sorted array S");
   for(int i=0;i&lt;S.length;i++)
   {for(int j=0;j&lt;2;j++)
   {if(j&lt;1){System.out.print(S[i][j]);
             System.out.print(",");


   }
   else{System.out.println(S[i][j]);

   }
   }
}

   for(int i=0;i&lt;R.length;i++)
    {
        for(int j=i+1;j&lt;R.length;j++)
        {if(R[i][0]&gt;R[j][0])
        {for(int k=0;k&lt;3;k++ )
        {temp_R[i][k]=R[i][k];
         R[i][k]=R[j][k];
         R[j][k]=temp_R[i][k];
        }}
        else
            continue;
        }
    }

   System.out.println("Sorted array R");
   for(int i=0; i&lt; R.length;i++)
   {for(int j =0;j&lt;3;j++)
   {if(j&lt;2){System.out.print(R[i][j]);
   System.out.print(",");
   }
   else{System.out.println(R[i][j]);
   }
}
}  
   // Moving S into memory

   int def_mem_Size = 0;
   for (int i =0; i&lt;S.length; i++){
       for (int k = 0; k &lt; 5 ; k++){
           if(S[i][0] == M[k][0] &amp;&amp; S[i][1] == M[k][1] 
                       ){
           M[k][3] = M[k][3] + 1;
                   break;
               }
           if(k == def_mem_Size){
                for (int p =0 ; p&lt;2 ;p++)
                    M[k][p] = S[i][p];
               def_mem_Size++;
               break;
           }
       }

   }

   System.out.println("Content of M");
   for(int i=0; i&lt;  def_mem_Size;i++)
   {for(int j =0;j&lt;3;j++)
   {if(j&lt;2){System.out.print(M[i][j]);
   System.out.print(",");
   }
   else{System.out.println(M[i][j]);
   }
}
}

 int count=0;



 for(int i =0;i&lt;R.length;i++)
{ int mh=0;
int location = -1;
    for(int k=0;k&lt;def_mem_Size;k++){
    {if(R[i][1]==M[k][0])
        {mh=1;
        location=k;
        break;  
    }
    }
    }

   if(mh==1){
       for(int h=0;h&lt;2;h++)

     Output[count][h]=M[location][h];
                       Output[count][2]=R[location][2];
                           count++;
    }
    }





for (int p =0; p&lt; count; p++ ){
    for (int l =0 ; l&lt;4;l++)
            System.out.print(Output[p][l]+" ");
    System.out.println();

}
    }











The Output is 
Sorted array S
1,2
4,6
4,5
8,9
Sorted array R
1,2,3
2,4,6
2,3,4
3,4,5
3,4,5
4,7,8
8,9,1
Content of M
1,2,0
4,6,0
4,5,0
8,9,0

4 6 6 0 
4 6 6 0 
4 6 6 0 
</code></pre>