1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public class RecursifDichotomique
{
public static void main(String[] args)
{
RecursifDichotomique rd = new RecursifDichotomique();
int[] tableau = {2,4,5,12,14,15,17,23,52,65,76,86,89,91,95,102,103};
System.out.println(rd.recursifDichotomique(0, tableau.length, tableau, 3));
}
public int recursifDichotomique(int debut, int fin, int[]tableau, int cle)
{
//clause de finitude
if(debut > fin)
{
return -1;
}
if(tableau[(debut+fin)/2] == cle)
{
return (debut+fin)/2;
}
if(cle < tableau[(debut+fin)/2])
{
return recursifDichotomique(debut, ((debut+fin)/2), tableau, cle);
}
else
{
return recursifDichotomique(((debut+fin)/2), fin, tableau, cle);
}
}
} |
Partager