Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. private int trouverIndice(int unEntier){
  2.         //recherche sequentielle
  3.         for (int i = 0; i < this.nombreDEntiers; i++) {
  4.             if(this.tableDEntiers[i]==unEntier)
  5.                 return i;
  6.             if(this.tableDEntiers[i]>unEntier)
  7.                 return -1;
  8.         }
  9.         return -1;
  10.     }
  11.  
  12.    
  13.     /* fonction de recherche dichotomique qui renvoie un indice où se trouve la valeur "val" si elle est dans le tableau "tab[]" et -1 si cette valeur n'y est pas */
  14.     private int rechercheDicho(int tab[], int nbVal, int val){
  15.  
  16.    
  17.       boolean trouve;  
  18.       int id;
  19.       int ifin;  
  20.       int im;  
  21.      
  22.    
  23.       trouve = false;  
  24.       id = 0;  
  25.       ifin = nbVal;
  26.      
  27.       /* boucle de recherche */
  28.       while(!trouve && ((ifin - id) > 1)){
  29.  
  30.         im = (id + ifin)/2;
  31.        
  32.         trouve = (tab[im] == val);
  33.        
  34.         if(tab[im] > val) ifin = im;
  35.       }
  36.      
  37.       /* test conditionnant la valeur que la fonction va renvoyer */
  38.       if(tab[id] == val) return(id);  
  39.       else return(-1);
  40.      
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement