Thread: bubble sort in a linked list

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    bubble sort in a linked list

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

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    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);
    }
    The list is setup like this
    Code:
    typedef struct list{int val; struct list *n;}LIST;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM