first commit with existing project files
This commit is contained in:
28
algo_js/tableau_dichotomie.js
Normal file
28
algo_js/tableau_dichotomie.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const prompt = require('prompt-sync')();
|
||||
|
||||
// Recherche dichotomique
|
||||
|
||||
let table = [1,5,23,26,35,39,46,53,68,79,90];
|
||||
let nombre = parseInt(prompt("Entrez un nombre à rechercher: "));
|
||||
|
||||
let debut = 0;
|
||||
let fin = table.length - 1;
|
||||
45
|
||||
while (debut <= fin) {
|
||||
|
||||
if (!table.includes(nombre)) {
|
||||
console.log(nombre + " n'est pas dans le tableau !")
|
||||
break;
|
||||
}
|
||||
|
||||
let milieu = Math.floor((debut + fin) / 2);
|
||||
console.log("Recherche entre " + table[debut] + " et " + table[fin]);
|
||||
|
||||
if (table[milieu] === nombre) {
|
||||
console.log("Trouvé ! : " + nombre)
|
||||
break;
|
||||
|
||||
} else if (table[milieu] < nombre) debut = milieu + 1;
|
||||
|
||||
else fin = milieu - 1;
|
||||
}
|
||||
Reference in New Issue
Block a user