summaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1992-05-11 06:15:01 +0000
committerRoland McGrath <roland@gnu.org>1992-05-11 06:15:01 +0000
commit3df46439c0ee75493d8872c4d17e3efc18c1de4d (patch)
tree7701facdc6d0fab93c2ef0598b92f97e8aee3933 /stdlib
parent073588c4ec6afeb68616afb5a3a929b78a0de3be (diff)
Formerly ../stdlib/testsort.c.~2~
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/testsort.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/stdlib/testsort.c b/stdlib/testsort.c
index 5007b9e86e..fb6271c88d 100644
--- a/stdlib/testsort.c
+++ b/stdlib/testsort.c
@@ -4,6 +4,12 @@
#include <stdio.h>
int
+DEFUN(compare, (a, b), CONST PTR a AND CONST PTR b)
+{
+ return strcmp (*(char **) a, *(char **) b);
+}
+
+int
DEFUN_VOID(main)
{
static char *lines[500];
@@ -11,14 +17,22 @@ DEFUN_VOID(main)
size_t i;
i = 0;
- while (i < 500 && getline (&lines[i], &lens[500]) > 0)
+ while (i < 500 && getline (&lines[i], &lens[500], stdin) > 0)
++i;
if (i < 500)
lines[i] = NULL;
- qsort (lines, 500, sizeof (char *), strcmp);
+ while (--i > 0)
+ {
+ size_t swap = random () % i;
+ char *line = lines[swap];
+ lines[swap] = lines[i];
+ lines[i] = line;
+ }
+
+ qsort (lines, 500, sizeof (char *), compare);
- while (i < 500 && lines[i] != NULL)
+ for (i = 0; i < 500 && lines[i] != NULL; ++i)
fputs (lines[i], stdout);
return 0;