1:单链表冒泡排序
单链表
struct node;
{
int i;
node* pnode;
}NODE;
NODE *bubblesort(NODE *head)
{
NODE *q,*tail,*p=(NODE*)malloc(sizeof(NODE));
p->next = head;
head = p;
tail = NULL;
while(tail != head->next)
{
p = head;
q =p->next;
while(q->next != tail)
{
if (p->next->i < q->next->i)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
}
p = p->next;
q = p->next;
}
tail = q;
}
p = head->next;
free(head);
return p;
}
2:快速排序
struct LIST;
{
int i;
LIST* next;
}NODE;
LIST qsortL (LIST h, LIST lnext)
{
NODE n1, n2;
LIST p, t1 = &n1, t2 = &n2;
if (h == NULL) return lnext;
for (p = h->next; p != NULL;
p = p->next)
if (p->i < h->i)
{
t1->next = p;
t1 = p;
}
else
{
t2->next = p;
t2 = p;
}
t1->next = t2->next = NULL;
h->next = qsortL(n2.next, lnext);
return qsortL(n1.next, h);
}
单链表
struct node;
{
int i;
node* pnode;
}NODE;
NODE *bubblesort(NODE *head)
{
NODE *q,*tail,*p=(NODE*)malloc(sizeof(NODE));
p->next = head;
head = p;
tail = NULL;
while(tail != head->next)
{
p = head;
q =p->next;
while(q->next != tail)
{
if (p->next->i < q->next->i)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
}
p = p->next;
q = p->next;
}
tail = q;
}
p = head->next;
free(head);
return p;
}
2:快速排序
struct LIST;
{
int i;
LIST* next;
}NODE;
LIST qsortL (LIST h, LIST lnext)
{
NODE n1, n2;
LIST p, t1 = &n1, t2 = &n2;
if (h == NULL) return lnext;
for (p = h->next; p != NULL;
p = p->next)
if (p->i < h->i)
{
t1->next = p;
t1 = p;
}
else
{
t2->next = p;
t2 = p;
}
t1->next = t2->next = NULL;
h->next = qsortL(n2.next, lnext);
return qsortL(n1.next, h);
}