-
sort function help
hi, in this sort function i need some help.....is this sort function sorting by swapping the values in the nodes, or does a swap by swapping the nodes itself , after comparing the values?
Code:
void dList<Type>::sort()
{
for(nodeType<Type>* temp=first;temp != NULL; temp=temp->next)
{
nodeType<Type>* min = temp;
for(nodeType<Type>* current = min; current != NULL; current=current->next)
{
if(current->info < min->info)min=current;
}
Type temp_var=temp->info;
temp->info=min->info;
min->info=temp_var;
}
}
-
It swaps the values.Modify this sort function so it swaps the nodes insteed (improves performance).
P.S.
change this line to
Code:
for(nodeType<Type>* current = min; current != NULL; current=current->next)
Code:
for(nodeType<Type>* current = min->next; current != NULL; current=current->next)
because you are comparing two identical values each time you enter this for-loop.