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]]] )
$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 |
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 |
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�.
#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)
; 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