#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define ID_DATANODE 1001
struct node {
int value;
struct node *next;
};
void push(struct node **root, int value) {
struct node *p;
if ((p = xmalloc(sizeof(struct node), ID_DATANODE)) != 0) {
p->value = value;
p->next = *root;
*root = p;
}
}
int pop(struct node **root, int *value) {
int r;
struct node *p;
r = 0;
if (*root != 0) {
p = *root;
*root = p->next;
*value = p->value;
xfree(p, ID_DATANODE);
r = 1;
}
return r;
}
void sub_swap(struct node **p, struct node **q) {
struct node *t, *s;
if (&((*p)->next) == q) {
t = *p;
s = (*q)->next;
*p = (*p)->next;
(*q)->next = t;
*q = s;
} else {
t = *p;
*p = *q;
*q = t;
s = (*p)->next; /* in fact *p<=>*q but neet to do (*p)->next <=> (*q)->next */
(*p)->next = (*q)->next;
(*q)->next = s;
}
}
void combsort(struct node **root, int n, int (*comp)(int, int)) {
struct node **p, **q;
int i, h, flag, c;
c = 0;
h = n;
do {
/* h = (int)(h / 1.247330950103979); */
h = 1; /* force to bubble-sort. */
if (h == 10)
h = 11;
if (h < 1)
h = 1;
fprintf(stderr
, "%d:%d\n", ++c
, h
); flag = 0;
for (q = root, i = 0; i < h; q = &((*q)->next), i++)
;
p = root;
for (;;) {
if (comp((*p)->value, (*q)->value) > 0) {
flag = 1;
sub_swap(p, q);
}
if (*p == 0 || *q == 0 || (*p)->next == 0 || (*q)->next == 0)
break;
p = &((*p)->next);
q = &((*q)->next);
}
} while (flag);
}
int cmp_ascent(int a, int b) {
return a - b;
}
int isSortOK(struct node *root) {
if (root->next) {
if (root->value <= root->next->value) {
return isSortOK(root->next);
} else {
return 0;
}
} else {
return 1;
}
}
#define SEED 31415926
#define N 100
int main(int argc, char *argv[]) {
struct node *root;
int i, v, flag;
root = 0;
for (i = 0; i < N; i++)
push
(&root
, (rand() / (RAND_MAX
+ 1.0) * 1000.0));
combsort(&root, i, cmp_ascent);
flag = isSortOK(root);
i = 0;
while (pop(&root, &v) != 0)
printf("%3d : %4d\n", ++i
, v
);
if (flag)
else
fprintf(stderr
, "NG, there are some bugs in this program.\n"); xmallocdump();
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8YXNzZXJ0Lmg+CiAKLyogI2RlZmluZSBERUJVRyAqLwojaWYgZGVmaW5lZChERUJVRykKI2luY2x1ZGUgInhtYWxsb2MuaCIKI2Vsc2UKI2RlZmluZSB4bWFsbG9jKHgsIHkpIG1hbGxvYyh4KQojZGVmaW5lIHhmcmVlKHgsIHkpIGZyZWUoeCkKI2RlZmluZSB4cmVhbGxvYyh4LCB5LCB6KSByZWFsbG9jKHgsIHkpCiNkZWZpbmUgeG1hbGxvY2R1bXAoKQojZW5kaWYKI2RlZmluZSBJRF9EQVRBTk9ERSAxMDAxCgpzdHJ1Y3Qgbm9kZSB7CiAgaW50IHZhbHVlOwogIHN0cnVjdCBub2RlICpuZXh0Owp9OwoKdm9pZCBwdXNoKHN0cnVjdCBub2RlICoqcm9vdCwgaW50IHZhbHVlKSB7CiAgc3RydWN0IG5vZGUgKnA7CiAgaWYgKChwID0geG1hbGxvYyhzaXplb2Yoc3RydWN0IG5vZGUpLCBJRF9EQVRBTk9ERSkpICE9IDApIHsKICAgIHAtPnZhbHVlID0gdmFsdWU7CiAgICBwLT5uZXh0ID0gKnJvb3Q7CiAgICAqcm9vdCA9IHA7CiAgfQp9CgppbnQgcG9wKHN0cnVjdCBub2RlICoqcm9vdCwgaW50ICp2YWx1ZSkgewogIGludCByOwogIHN0cnVjdCBub2RlICpwOwogIHIgPSAwOwogIGlmICgqcm9vdCAhPSAwKSB7CiAgICBwID0gKnJvb3Q7CiAgICAqcm9vdCA9IHA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