Update list sort

This commit is contained in:
Gregory Lirent 2022-06-05 18:35:14 +03:00
parent 2f62ee51e7
commit 79935eb0b4

View File

@ -16,78 +16,38 @@ static inline void lnode_swap(lnode_t* s0, lnode_t* s1) {
s1->type = t;
}
static inline lnode_t* mid(lnode_t* l, lnode_t *r) {
while (l != r) {
if ((r = r->prev) == l)
break;
l = l->next;
}
return l;
}
static inline lnode_t* pivot(lnode_t *l, lnode_t *r) {
lnode_t *m = mid(l, r);
lnode_t *c = l->prev;
if (lnode_compare(m, l) < 0)
lnode_swap(m, l);
if (lnode_compare(m, r) > 0) {
lnode_swap(m, r);
if (lnode_compare(m, l) < 0)
lnode_swap(m, l);
for (lnode_t* i = l; i != r; i = i->next) {
if (lnode_compare(i, r) < 0) {
c = (is_null(c)) ? l : c->next;
lnode_swap(c, i);
}
}
return m;
c = (is_null(c)) ? l : c->next;
lnode_swap(c, r);
return c;
}
/*#####################################################################################################################*/
static void quick_sort(lnode_t* l, lnode_t* r) {
lnode_t* p;
static void quick_sort(lnode_t *l, lnode_t *r) {
if (!is_null(r) && l != r && l != r->next) {
p = pivot(l, r);
lnode_t *c;
lnode_t *m = pivot(l, r);
lnode_t *n = l;
lnode_t *p = r;
int f = 0;
for (;;) {
while (lnode_compare(l, m) < 0) {
l = l->next;
if (p == l) /* l >= p */
f |= 0x01;
}
c = l->prev;
while (lnode_compare(r, m) > 0) {
r = r->prev;
if (n == r) /* n >= r */
f |= 0x02;
if (c == r) /* l > r */
f |= 0x04;
}
if (!(f&0x04)) { /* l <= r */
lnode_swap(l, r);
l = l->next;
r = r->prev;;
} else break;
quick_sort(l, p->prev);
quick_sort(p->next, r);
}
if (!(f&0x02)) /* n < r */
quick_sort(n, r);
if (!(f&0x01)) /* l < p */
quick_sort(l, p);
}
/*#####################################################################################################################*/
void list_sort(list_t* x) {
if (x->first != x->last)
return quick_sort(x->first, x->last);;
return quick_sort(x->first, x->last);
}