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
| [int] $leCode = 36
$t = (1,2,4,5,6,7,8,10,13,14,16,18,20,35,36,48,51,52,53,60)
[int] $sonIndice = -1
[Boolean] $trouve = $false
[int] min = 0
[int] $max =$t.Length
[int] $mid = 0
while( ($trouve = $false) and ($max >= $min) )
{
$mid = [math]::Floor(($max + $min) / 2) # ligne sans erreur
if($t[mid] -eq $leCode)
$trouve = "true"
$sonIndice = $mid
} else {
if($t[$mid] -gt $lecode) {
$max = $mid - 1
} else {
$min = $mid + 1
}
}
}
Write-Host $t
Write-Host "l'indice dans le tableau est "
$sonIndice |
Partager