testsort.c 602 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. static int
  5. compare (const void *a, const void *b)
  6. {
  7. return strcmp (*(char **) a, *(char **) b);
  8. }
  9. int
  10. main (void)
  11. {
  12. char bufs[500][20];
  13. char *lines[500];
  14. size_t lens[500];
  15. size_t i, j;
  16. srandom (1);
  17. for (i = 0; i < 500; ++i)
  18. {
  19. lens[i] = random() % 19;
  20. lines[i] = bufs[i];
  21. for (j = 0; j < lens[i]; ++j)
  22. lines[i][j] = random() % 26 + 'a';
  23. lines[i][j] = '\0';
  24. }
  25. qsort (lines, 500, sizeof (char *), compare);
  26. for (i = 0; i < 500 && lines[i] != NULL; ++i)
  27. puts (lines[i]);
  28. return 0;
  29. }