/* Recherche de la presence d'une valeur entiere */ /* dans un tableau d'entiers trie */ /* par ordre croissant */ /* Methode sequentielle */ public class RecherchePresenceMethodeSequentielle { /////////////////////////////////////////////////// /* Recherche sequentielle de la presence */ /* d'un int dans un tableau de int trie */ static boolean estPresent(int v,int [] t) { boolean run; boolean res; int i; run = true; i = 0; while ( run ) { if ( i == t.length ) run = false; else { if ( t[i] >= v ) { run = false; } else { i = i+1; } } } res = false; if ( i < t.length ) { if ( t[i] == v ) { res = true; } } return res; } /* Recherche sequentielle de la presence */ /* d'un int dans un tableau de int trie */ /* Version optimisee */ static boolean estPresent2(int v,int [] t) { int i = 0; while ( ( i != t.length ) && ( t[i] < v ) ) { i = i+1; } return ( ( i < t.length ) && ( t[i] == v ) ); } /////////////////////////////////////////////////// /* Methode d'affichage des valeurs contenues */ /* dans un tableau de int */ static void affichageTableau(int [] t) { int i; for ( i = 0 ; i < t.length ; i = i+1 ) { Ecran.afficherln(t[i]," "); } } /* Methode de creation d'un tableau de 10 int */ /* initialise avec des valeurs croissantes */ static int [] initRandCroissant() { int [] t = new int[10]; int i; t[0] =(int) (Math.random()*3.0); for ( i = 1 ; i < t.length ; i = i+1 ) { t[i] = t[i-1] + (int) (Math.random()*3.0); } return t; } /* Programme principal */ public static void main(String [] args) { boolean present; int [] t; int v; t = initRandCroissant(); affichageTableau(t); Ecran.afficher("SVP, valeur à rechercher : "); v = Clavier.saisirInt(); present = estPresent(v,t); Ecran.afficherln(v," present? : ",present); } }