Skip to content
Snippets Groups Projects
Select Git revision
  • afb47a1dd898b593df2db2a6d2d1f0121fdea375
  • master default
2 results

loesung-3-20170206-1.c

Blame
  • Forked from Peter Gerwinski / hp
    326 commits behind the upstream repository.
    loesung-3-20170206-1.c 759 B
    #include <stdio.h>
    
    void sort (void **data, int (*compare) (void *x1, void *x2))
    {
      for (int i = 0; data[i]; i++)
        for (int j = i + 1; data[j]; j++)
          if (compare (data[i], data[j]) > 0)
            {
              void *tmp = data[i];
              data[i] = data[j];
              data[j] = tmp;
            }
    }
    
    int main (void)
    {
      char *strings[] = { "Thomas", "Dora", "Konrad", "Adalbert", "Sophie", NULL };
      sort (strings, compare_strings);
      for (int i = 0; strings[i]; i++)
        printf ("%s\n", strings[i]);
    
      printf ("\n");
    
      int two = 2, ten = 10, zero = 0, three = 3, one = 1;
      int *numbers[] = { &two, &ten, &zero, &three, &one, NULL };
      sort (numbers, compare_numbers);
      for (int i = 0; numbers[i]; i++)
        printf ("%d\n", *numbers[i]);
    
      return 0;
    }