how can i write a bubble sort function for a linked list can u give me an example??
This is a discussion on bubble sort in a linked list within the C++ Programming forums, part of the General Programming Boards category; how can i write a bubble sort function for a linked list can u give me an example??...
how can i write a bubble sort function for a linked list can u give me an example??
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.
The list is setup like thisCode: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;