LordSputnik

Bubble Sort Test Code

Apr 27th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. struct Employee* first; //The employee database.
  2.  
  3. static void sort_db()
  4. {
  5.   struct Employee* current, *next, *tmp;
  6.   int swaps;
  7.   current = first;
  8.  
  9.   if( current == NULL )
  10.     return;
  11.  
  12.   next = current->next;
  13.  
  14.   do
  15.   {
  16.     current = first;
  17.     next = current->next;
  18.    
  19.     swaps = 0;
  20.     while ( next != NULL )
  21.     {
  22.       if ( strcmp ( current->name, next->name ) > 0 )
  23.       {
  24.         swaps++;
  25.  
  26.         if ( current->prev != NULL )
  27.           current->prev->next = next;
  28.         if ( next->next != NULL )
  29.           next->next->prev = current;
  30.        
  31.         next->prev = current->prev;
  32.         current->next = next->next;
  33.        
  34.         next->next = current;
  35.         current->prev = next;
  36.  
  37.         if ( current == first )
  38.           first = next;
  39.         if ( next == last )
  40.           last = current;
  41.  
  42.         tmp = next;
  43.         next = current;
  44.         current = tmp;
  45.       }
  46.  
  47.       current = next;
  48.       next = current->next;
  49.     }
  50.   } while ( swaps != 0 );
  51. }
Advertisement
Add Comment
Please, Sign In to add comment