Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chercher;
- public class Chercher {
- /*
- * La fonction chercherElt permet de chercher un element dans un tableau
- * chercherElt se crée grâce à la dichotomie du tableau
- * Qui par la suite va chercher l'élément dans le tableau si il existe
- */
- public static int chercherElt(int element, int [] tab){
- int dichotomie = tab.length/2 ;
- if(tab[dichotomie] == element) {
- return dichotomie;
- }
- if (tab[dichotomie]>element) {
- int[] tabTmp = new int[dichotomie];
- System.arraycopy(tab, 0, tabTmp, 0, dichotomie);
- return chercherElt(element, tabTmp) ;
- }
- else {
- int[] tabTmp = new int[dichotomie];
- if(tab.length % 2 != 0) {
- System.arraycopy(tab, dichotomie + 1, tabTmp, 0, tab.length - (dichotomie + 1));
- return dichotomie + 1 + chercherElt(element, tabTmp);
- }
- System.arraycopy(tab, dichotomie, tabTmp, 0, tab.length - (dichotomie));
- return dichotomie + chercherElt(element, tabTmp);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement