#include #include #define lengthof(x) (sizeof(x) / sizeof(*(x))) int compare(const void *a_, const void *b_) { int const* a = a_; int const* b = b_; #if 0 // Kann zu Überlauf führen, z.B. -2.000.000.000 - 2.000.000.000 return *b - *a; #else // Sichere Alternative, Vergleichsergebnisse sind stets 1 oder 0 return (*b > *a) - (*b < *a); #endif } int main(void) { int a[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 }; qsort(a, lengthof(a), sizeof(*a), compare); int* p; for (p = a; p != a + lengthof(a); ++p) printf("%d ", *p); printf("\n"); }