2017-10-17 7 views
2

J'essaie de créer un algorithme de recherche binaire et j'ai utilisé deux ensembles d'instructions if lorsque l'échantillon est pair/impair. Le côté inégal travaille actuellement comme prévu et renvoie true, les rendements même côté vrai mais va à la « fourre-tout » morceau de code en bas de la fonction et retourne false:Le code semble continuer à s'exécuter après l'instruction return (en C)

bool search(int value, int values[], int n) 
{ 
    //searching algorithm 
    if (n <= 0) 
    { 
     return false; 
    } 
    //searching algorithm where n is even, if b is not the searched for value, split the sample and run recursively until value is equal to b or n<=0 
    if (n % 2 == 0) 
    { 
     int starte = n/2; 
     eprintf("starte is %i", starte); 
     int startpluse = starte + 1; 
     int b = values[starte]; 
     eprintf("b is %i", b); 
     //for (int i=0; i<n; i++){ 
     //printf("%i,",values[i]);} 
     if (b == value) 
     { 
      printf("true\n"); 
      return true; 
     } 
     else 
     { 
      if (value > b) 
      { 
       int o = starte - 1; 
       int searcharrayc[o]; 
       for (int h = startpluse, l = 0; l < o; h++, l++) 
       { 
        searcharrayc[l] = values[h]; 
       } 
       search(value, searcharrayc, o); 
      } 
      if (value < b) 
      { 
       int searcharrayd[starte]; 
       for (int m = 0; m < starte; m++) 
       { 
        searcharrayd[m] = values[m]; 
       } 
       search(value, searcharrayd, starte); 
      } 
     } 
    } 
    //searching algorithm where n is uneven, if a is not the searched for value, split the sample and run recursively until a is equal to the value or n<=0 
    if (n % 2 == 1) 
    { 
     eprintf("n is %i", n); 
     int start = (n/2) - 0.5; 
     int startplus = start + 1; 
     int a = values[start]; 
     eprintf("a is %i", a); 
     if (a == value) 
     { 
      return true; 
     } 
     else 
     { 
      if (value > a) 
      { 
       int searcharray[start]; 
       for (int i = startplus, j = 0; j < start; i++, j++) 
       { 
        searcharray[j] = values[i]; 
        eprintf("i is %i", i); 
       } 
       search(value, searcharray, start); 
      } 
      if (value < a) 
      { 
       int searcharrayb[start]; 
       for (int k = 0; k < start; k++) 
       { 
        searcharrayb[k] = values[k]; 
        eprintf("k is %i", k); 
       } 
       search(value, searcharrayb, start); 
      } 
     } 
    } 
    return false; 
} 
+1

Lorsque vous faites un nouvel appel à search(), vous devez utiliser return search() – webbi

+1

'if' instruction n'est pas nécessaire. Vous devez déterminer les plages sur lesquelles cela va fonctionner. la parité ne vient pas en image ici. – coderredoc

+0

Bien sûr que oui. Le programme reprend à partir de la pile d'appels. Une fois que 'search (value, searcharray, start);' est terminé, l'évaluation continuera dans votre programme. Ainsi 'return false 'est exécuté. – JustinJmnz

Répondre

2

Votre code ressemble à ceci :

search(...) 
{ 
    if(cond) 
     return false 
    if(cond) 
     return true 
    else 
     search(...) 
    return false 
} 

Vous devez changer pour:

search(...) 
{ 
    if(cond) 
     return false 
    if(cond) 
     return true 
    else 
     return search(...) 
} 

Notez le retour supplémentaire avant l'appel récursif à la recherche