summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2010-01-31 08:08:55 +0000
committerDimitri Sokolyuk <demon@dim13.org>2010-01-31 08:08:55 +0000
commitbf4977895739977921c586a152c84027d1920a05 (patch)
treead45432502a5556a5fe97355797e001eb0f2b075
parente92c0f6e9459e3dcc1d1dc55e856c09e813e6976 (diff)
verbose output, random data
-rw-r--r--lsort.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/lsort.c b/lsort.c
index ef58cf2..c93b08d 100644
--- a/lsort.c
+++ b/lsort.c
@@ -18,6 +18,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <time.h>
typedef struct list List;
@@ -45,7 +46,8 @@ lsort(List *list, int (*compar)(const List *, const List *))
assert(list && compar);
- for (k = 1;; k *= 2) {
+ k = 1;
+ do {
p = list;
list = NULL;
listp = &list;
@@ -77,9 +79,9 @@ lsort(List *list, int (*compar)(const List *, const List *))
p = eq;
}
*listp = NULL;
- if (merges <= 1)
- break;
- }
+ lprint(">", list);
+ k *= 2;
+ } while (merges > 1);
return list;
}
@@ -96,19 +98,21 @@ lprint(char *pre, List *p)
}
int
-main(void)
+main(int argc, char **argv)
{
List *head, **headp, *p, *next;
- int order[] = {6, 2, 8, 4, 11, 1, 12, 7, 3, 9, 5, 0, 10};
- int i, n = sizeof(order) / sizeof(order[0]);
+ int i, n;
+
+ n = (argc == 2) ? atoi(argv[1]) : 13;
head = NULL;
headp = &head;
+ srandom(time(NULL));
for (i = 0; i < n; i++) {
*headp = malloc(sizeof(List));
assert(*headp);
- (*headp)->value = order[i];
+ (*headp)->value = random() % n;
headp = &(*headp)->link;
}
*headp = NULL;