how can i write a bubble sort function for a linked list can u give me an example??

Printable View

- 12-08-2002condorxbubble sort in a linked list
how can i write a bubble sort function for a linked list can u give me an example??

- 12-08-2002Pioneer
Why would you ever want to use bubble sort? Here's an insertion sort for linked lists, it's not as bad as bubble sort and the algorithm works better for lists.

Code:`LIST *sort(LIST *original){`

LIST listb = {0,0}, *a = original, *b = &listb, *t, *u, *v;

for (t = a->n; t != NULL; t->n = v->n, v->n = t, t = u)

for (u = t->n, v = b; v->n && v->n->val < t->val; v = v->n);

return(*original = listb, original);

}

Code:`typedef struct list{int val; struct list *n;}LIST;`