2010-06-02 6 views
0

ici est un code pseudo sur un algorithme pour trouver médiane de tableau à deuxquestion sur la médiane de tableau à deux

T WO -A RRAY-M EDIAN (X, Y) 
n ← length[X]     £ n also equals length[Y ] 
median ← F IND -M EDIAN (X, Y, n, 1, n) 
if median = NOT- FOUND 
    then median ← F IND -M EDIAN (Y, X, n, 1, n) 
return median 
F IND -M EDIAN (A, B, n, low, high) 
if low > high 
    then return NOT- FOUND 
    else k ← (low + high)/2 
     if k = n and A[n] ≤ B[1] 
      then return A[n] 
     elseif k < n and B[n − k] ≤ A[k] ≤ B[n − k + 1] 
      then return A[k] 
     elseif A[k] > B[n − k + 1] 
      then return F IND -M EDIAN (A, B, n, low, k − 1) 
     else return F IND -M EDIAN (A, B, n, k + 1, high) 

il est de l'introduction d'algorithmes MIT

et voici mon code s'il vous plaît si quelque chose ne va pas dans mon code me dire

public class findmedian{ 
public static int find(int []x,int []y){ 
int median; 
int n=x.length; 
    median=Median(x, y, n,0, n); 
    if (median<-100000){ 
    Median(y,x, n,1, n); 

} 

return median; 


} 


public static void main(String[]args){ 
int x[]=new int[]{3,5,7,8,5,9,10}; 
int y[]=new int[]{12,4,8,9,6,7,10}; 
find(x,y); 


} 

public static int Median(int []a,int []b,int n,int low,int high) 
{ 


    if (low>high) 
return -1 ; 
    int k=(low+high)/2; 
    if (k==n && a[n]<=b[1]) 
    return a[n]; 
else if (k<n && b[n-k]<=a[k] && b[n-k]<=b[n-k+1]) 
return a[k]; 
    else if (a[k]> b[n-k-1]) 
return Median(a,b,n,low,k-1); 





return Median(a,b,n,k+1,high); 





} 


} 

après le compiler ne retourne rien s'il vous plaît aider

+0

Si vous formatez votre code correctement, il sera plus facile de lire et de déboguer –

Répondre

1
if (low>high) 
return; 

Est-ce l'erreur? Vous devez retourner quelque chose. (Le type de retour est int)

+0

non seulement cela est la version complète de ce code –

+0

une idée? Aidez-moi, s'il vous plaît –