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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include<stdio.h>
static int G_count;
static int chercher (int const tab[], size_t n, int val)
{
#if 1
/* recherche lineaire pour tester le test */
int ndx = -1;
size_t i;
for (i = 0; ndx == -1 && i < n; i++)
{
if (tab[i] == val)
{
ndx = i;
/* debug */
G_count += i;
}
}
return ndx;
#endif
/* a toi d'ecrire le code 'dichotomique' */
}
static void afficher (int const tab[], size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
printf ("%4d", tab[i]);
}
printf ("\n");
}
int main (void)
{
int tab[] = { -3, -1, 0, 2, 5, 7 };
size_t const n = sizeof tab / sizeof *tab;
afficher (tab, n);
{
int i;
for (i = -5; i <= 10; i++)
{
int trouve = chercher (tab, n, i);
if (trouve != -1)
{
printf ("[%d] = %4d\n", trouve, tab[trouve]);
}
}
printf ("\n");
}
printf ("%d iterations\n", G_count);
return 0;
} |
Partager