2
Recherche linéaire
- #include <stdio.h>
- static void print( int tab[], const int siz )
- {
- int i;
- for ( i=0; i < siz; i++ )
- printf((i < siz-1) ? "%d, " : "%d\n", tab[ i ]);
- }
- int search( int val, int tab[], const int siz )
- { /* linear search */
- int i;
- for ( i=0; i < siz; i++ )
- if ( tab[i] == val )
- return i;
- return -1;
- }
- main() {
- int tab[] = { 5, 8, 2, 9, 1, 6, 4, 3, 7 };
- int siz = sizeof (tab) / sizeof (int);
- print( tab, siz );
- printf("1 -> %d\n", search( 1, tab, siz ) );
- printf("0 -> %d\n", search( 0, tab, siz ) );
- }
Commentaires