2017-07-21 1 views
0

J'essaie d'écrire le code de la recherche binaire en Java. Mais le curseur est pile et ne peut voir aucune réponse. S'il vous plaît j'ai besoin d'aide pour résoudre le problème. Je crois que le code que j'ai écrit est logique cependant. Pas certain!mon code BinarySearch ne fonctionne pas. Quel est le problème avec mon code?

public class BinarySearch { 
public static void main(String[] args) { 

    int test[] = {1, 3, 5, 6}; 

    // int ans = BinarySearch(test, 6); 

    System.out.println(BinarySearch(test, 1)); 
    //while (counter <=test.length){ 
    //System.out.println(test[counter]); 
    //counter++; 
} 

public static int BinarySearch(int searcharr[], int key) { 

    int left, fulllength, midpoint; 

    left = 0; 
    midpoint = 0; 
    fulllength = searcharr.length; 

    while (left <= fulllength) { 

     midpoint = (left + fulllength)/2; 

     if (searcharr[midpoint] == key) { 

      return searcharr[midpoint]; 

     } else if (searcharr[midpoint] > key) { 
      left = midpoint - 1; 
     } else { 
      fulllength = midpoint + 1; 
     } 
     // left++; 
    } 
    return -1; 
} 
} 

Répondre

1

Il est un très petit problème

// searcharr[midpoint] = 5 
// 5 > 1 , execute else if , mean 
// 1 3 5 6 
// ^should be new fulllength = mid-1 

// what you are doing is 
// 1 3 5 6 
// ^left = mid-1 which is wrong 

else if (searcharr[midpoint] > key) { 
      left = midpoint - 1; 
     }  
else { 
     fulllength = midpoint + 1; 
    } 

code afin final serait

while (left<=fulllength){ 

     midpoint = (left + fulllength)/2; 
     System.out.println(midpoint + " "+left+" "+fulllength); 

     if(searcharr[midpoint]==key) 
     { 
      //return searcharr[midpoint]; 
      return midpoint; // return position instead of value 
     } 
     else if (searcharr[midpoint]>key) 
     { 
      fulllength = midpoint - 1; 
     } 
     else 
     { 
      left = midpoint + 1; 
     } 
    }  
+0

Merci beaucoup Rahul et Paveneet_Singh. Il fonctionne maintenant! je vais vous envoyer un message rapide pour explication dans les détails – Ekaku509

0

Vous avez commencé votre clé élevée à l'index incorrect et la comparaison logique dans votre instruction if était incorrecte. Vous avez également modifié incorrectement votre gauche et votre longueur à la fin de chaque boucle. Ils sont supposés être échangés. Cela devrait fonctionner.

public static int BinarySearch(int searcharr[], int key) { 

int left, fulllength, midpoint; 

left = 0; 
midpoint = 0; 
fulllength = searcharr.length - 1; //fulllength starts at incorrect index. 

while (left <= fulllength) { 

    midpoint = (left + fulllength)/2; 

    if (searcharr[midpoint] == key) { 

     return searcharr[midpoint]; 
     // the comparison inside was incorrect 
    } else if (searcharr[midpoint] < key) { 
     left = midpoint + 1; //instead of midpoint - 1 
    } else { 
     fulllength = midpoint - 1; //instead of midpoint + 1 
    } 
    // left++; 
} 
return -1; 
}