Wednesday 28 June 2017

Bubble Sort Algorithms

mynode *bubbleSort(mynode *head, int count)
{
    int i, j;
    mynode *p0, *p1, *p2, *p3;

    for(i = 1; i < count; i++)
     {
         p0 = (struct node *)NULL;
         p1 = head;
         p2 = head->next;
        p3 = p2->next;

       for(j = 1; j <= (count - i); j++)
        {
           if(p1->value > p2->value)
            {
                  // Adjust the pointers...
                p1->next = p3;
               p2->next = p1;
              if(p0)p0->next=p2;

             // Set the head pointer if it was changed...
            if(head == p1)head=p2;
            // Progress the pointers
             p0 = p2;
             p2 = p1->next;
             p3 = p3->next!=(struct node *)NULL?p3->next: (struct node *)NULL;
          }
          else
            {
                 // Nothing to swap, just progress the pointers...
                 p0 = p1;
                 p1 = p2;
                p2 = p3;
                p3 = p3->next!=(struct node *)NULL?p3->next: (struct node *)NULL;
            }
        }
   }
return(head);
}

No comments:

Post a Comment