Advertisement
Guest User

Chercher

a guest
Jan 25th, 2020
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package chercher;
  2.  
  3. public class Chercher {
  4.  
  5. /*
  6.  * La fonction chercherElt permet de chercher un element dans un tableau
  7.  * chercherElt se crée grâce à la dichotomie du tableau
  8.  * Qui par la suite va chercher l'élément dans le tableau si il existe
  9.  */
  10.    
  11.  
  12.         public static int chercherElt(int element, int [] tab){
  13.             int dichotomie = tab.length/2 ;
  14.  
  15.             if(tab[dichotomie] == element) {
  16.                 return dichotomie;
  17.             }
  18.  
  19.             if (tab[dichotomie]>element) {
  20.                 int[] tabTmp = new int[dichotomie];
  21.  
  22.                 System.arraycopy(tab, 0, tabTmp, 0, dichotomie);
  23.  
  24.                 return chercherElt(element, tabTmp) ;
  25.             }
  26.  
  27.             else {
  28.                 int[] tabTmp = new int[dichotomie];
  29.  
  30.  
  31.  
  32.                 if(tab.length % 2 != 0) {
  33.                     System.arraycopy(tab, dichotomie + 1, tabTmp, 0, tab.length - (dichotomie + 1));
  34.                     return dichotomie + 1 + chercherElt(element, tabTmp);
  35.                 }
  36.  
  37.                 System.arraycopy(tab, dichotomie, tabTmp, 0, tab.length - (dichotomie));
  38.  
  39.  
  40.  
  41.                 return dichotomie + chercherElt(element, tabTmp);
  42.             }
  43.  
  44.         }
  45.  
  46.  
  47.        
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement