void bubble_sort(item *node)
{
item *first_prev,*first,*second,*temp;
int i, j, n = 1;
bool sorted = false;
//make these 3 pointers point to the head of the list
temp = node;
first = node;
first_prev = node;
//count the number of nodes in the list
while (first->next != NULL)
{
n++;
first= first->next;
}
for (i = 1; i <= n; i++)
{
first = temp;
first_prev = temp;
second = first->next;
for (j=1; j<=(n-i); j++)
{
if (first->val > second->val)
{
if (first == temp)
{
first->next = second->next;
second->next = first;
temp = second;
first = second;
}
else
{
first->next = second->next;
second->next = first;
first_prev->next = second;
first = second;
}
}
first_prev = first;
first = first->next;
second = first->next;
}
}
sorted = true;
PrintList();
}
|