No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher dans un tableau...

Recherche séquentielle

La méthode la plus simple pour trouver une donnée dans un tableau est la recherche séquentielle: nous parcourons les éléments du tableau dans l'ordre d'index jusqu'au moment ou la donnée correspond au critère de recherche.

Exemple : recherche séquentielle

nmax=20; //nombre maximum elements du tableau

trouve=false;
k=0;//position dans le tableau

while ((k<nmax) && (!trouve))
{
if (tableau[k] == valeur_cherchee) trouve=true;
else k++;
}
if (k<nmax) printf("Valeur %d trouvee en emplacement %d.\n",valeur_cherchee,k);
else printf("Valeur non trouvee\n");

Contents Haut

Recherche binaire

Une recherche séquentielle peut nécessiter un temps de travail assez long en fonction de la taille du tableau, dans le cas, par exemple, où la valeur cherchée se trouve en fin de tableau.

Imaginons un instant que les valeurs du tableau apparaissent selon un ordre constant (par exemple, ordre croissant)...

Nous devons alors utiliser une méthode de recherche binaire.
Ce type de recherche porte le nom de binaire, car le principe est, à chaque étape, la division en deux du nombre de valeurs à trier.

Exemple : recherche binaire

#include <stdio.h>
#define nmax 100

int recherche_b(int t[], int val, int taille)
{
  int trouve=false;
  int
}

void main (void)
{
int t[nmax],a;
for (k=0;k<nmax;k++)
{
t[k]=k;
}
scanf ("nbre ?\n", &a);
recherche_b(t,a,nmax);
modification

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 21/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/c-recherche-tableau.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.