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
|
#include <stdio.h>
#include <string.h>
void aff (void const **tab, int n)
{
int i;
for (i = 0; i < n; i++)
{
printf ("%s ", (char *) tab[i]);
}
printf ("\n");
}
void tri_quest2 (void const **tab, int n,
int (*cmp_element) (void const *e1, void const *e2))
{
int sorted;
do
{
int j;
sorted = 1;
for (j = 0; j < n - 1; j++)
{
if (cmp_element (tab[j], tab[j + 1]) > 0)
{
/* swap */
{
void const *aux = tab[j + 1];
tab[j + 1] = tab[j];
tab[j] = aux;
}
sorted = 0;
aff (tab, n);
}
}
}
while (!sorted);
}
static int compare (void const *a, void const *b)
{
return strcmp (a, b);
}
int main (void)
{
void const *tab[] = { "d", "c", "b", "a" };
tri_quest2 (tab, 4, &compare);
puts ("");
aff (tab, 4);
return 0;
} |
Partager