summaryrefslogtreecommitdiff
path: root/lsort.c
diff options
context:
space:
mode:
Diffstat (limited to 'lsort.c')
-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;