Advertisement
Guest User

Untitled

a guest
Nov 14th, 2009
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. static void sort(struct linked_list *list,
  2.     int (*cmp)(const void *, const void *))
  3. {
  4.     size_t slice_size = 1;
  5.     for(; slice_size < list->size; slice_size *= 2)
  6.     {
  7.         struct node *tail = list->first;
  8.         while(tail)
  9.         {
  10.             struct node *head = tail;
  11.  
  12.             size_t count = slice_size;
  13.             while(tail && count--) // performance killer
  14.                 tail = tail->next;
  15.  
  16.             count = slice_size;
  17.             while(head != tail && tail && count)
  18.             {
  19.                 if(cmp(head->data, tail->data) <= 0)
  20.                     head = head->next;
  21.                 else
  22.                 {
  23.                     struct node *node = tail;
  24.                     tail = tail->next;
  25.                     remove_node(node, list);
  26.                     insert_before(node, list, head);
  27.                     --count;
  28.                 }
  29.             }
  30.  
  31.             while(tail && count--) // performance killer
  32.                 tail = tail->next;
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement