I was working on a sorting algorithm for my linked list implementation and wanted to get other people's input/comments/critique. What do you think of it in all aspects, including style?
I was told that it is preferred to use a bool type for the changeFlag
instead of using an int as it makes it more readable. Thoughts?
void LinkedList::sort()
{
if (head != 0)
{
Node* current = head;
Node* prev = 0;
Node* tempNode = 0;
bool changeFlag = false;
for (int i = 0; i < length; i++)
{
while (current->next != 0)
{
tempNode = current->next;
if (current->value > tempNode->value)
{
changeFlag = true;
current->next = tempNode->next;
tempNode->next = current;
if (prev != 0)
prev->next = tempNode;
prev = tempNode;
if (head == current)
head = tempNode;
if (current->next == 0)
end = current;
}
else
{
prev = current;
current = current->next;
}
}
if (changeFlag == false)
break;
else
{
prev = 0;
current = head;
changeFlag = false;
}
}
}
}