UDF > Array >


_ArrayBinarySearch

Utilise l'algorithme de recherche binaire dans un tableau trié 1D ou 2D

#include <Array.au3>
_ArrayBinarySearch ( Const ByRef $aArray, $vValue [, $iStart = 0 [, $iEnd = 0 [, $iColumn = 0]]] )

Paramètres

$aArray Tableau dans lequel s'effectuera la recherche
$vValue Valeur à rechercher
$iStart [optionnel] Index du tableau où la recherche doit commencer
$iEnd [optionnel] Index du tableau où la recherche doit s'arrêter
$iColumn [optionnel] Colonne du tableau où lancer la recherche

Valeur de retour

Succès: Retourne l'index de la valeur qui a été trouvée
Échec: Retourne -1, et définit @error <> 0
@error: 1 - $aArray n'est pas un tableau
2 - $vValue est en dehors des valeurs min/max du tableau
3 - $vValue na pas été trouvée dans le tableau
4 - $iStart est plus grand que $iEnd
5 - $aArray n'est pas un tableau 1D ou 2D
6 - $aArray est vide
7 - $iColumn est en dehors des limites du tableau

Remarque

Quand vous faites une recherche binaire dans un tableau d'éléments, le tableau (la colonne spécifié si 2D) DOIT être trié avant que la recherche soit faite. Dans le cas contraire un résultat imprévu sera retourné.

En relation

_ArrayFindAll, _ArraySearch

Exemples

Exemple 1

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $iIndex

Local $aArray[5][2]
For $i = 0 To 4
    For $j = 0 To 1
        $aArray[$i][$j] = "#" & String($i) & String($j)
    Next
Next
_ArrayDisplay($aArray, "Tableau")

; Recherche col 0
$iIndex = _ArrayBinarySearch($aArray, "#10", 0, 0, 0)
MsgBox($MB_SYSTEMMODAL, "Index", $iIndex)

; Recherche col 1
$iIndex = _ArrayBinarySearch($aArray, "#31", 0, 0, 1)
MsgBox($MB_SYSTEMMODAL, "Index", $iIndex)

Exemple 2

; Utilise un tableau retourné par StringSplit()

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $avArray = StringSplit("a,b,d,c,e,f,g,h,i", ",")

; Trie le tableau pour faire une recherche binaire
_ArraySort($avArray, 0, 1) ; Commence à l'index 1 pour sauter $avArray[0]

; Affiche le tableau trié
_ArrayDisplay($avArray, "$avArray APRES _ArraySort()")

; Commence à l'index 1 pour ignorer $avArray[0]
Local $iKeyIndex = _ArrayBinarySearch($avArray, "c", 1)
If Not @error Then
    MsgBox($MB_SYSTEMMODAL, 'Élément trouvé', 'Index: '& $iKeyIndex)
Else
    MsgBox($MB_SYSTEMMODAL, 'Élément non trouvé', 'Erreur: '& @error)
EndIf