21
Tri à bulle
- #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 ]);
- }
- #define SWAP(x, y) { int t = x; x = y; y = t; }
- void sort( int tab[], const int siz )
- { /* bubble sort */
- int i, j;
- for ( i=siz-1; i > 0; i-- ) {
- print( tab, siz );
- for ( j=0; j < i; j++ )
- if ( tab[j] > tab[j+1] )
- SWAP( tab[j], tab[j+1] );
- }
- }
- main() {
- int tab[] = { 5, 9, 8, 2, 9, 1, 6, 4, 3, 7, 1 };
- int siz = sizeof (tab) / sizeof (int);
- printf("Sorting %d integers:\n", siz);
- sort( tab, siz );
- print( tab, siz );
- }
Commentaires