I have this sort, can someone tell me why the code it not sorted correctly. Here is an example run of it.
Enter coefficient and exponent (0 0 to stop):9 8
Enter coefficient and exponent (0 0 to stop):3 10
Enter coefficient and exponent (0 0 to stop):6 2
Enter coefficient and exponent (0 0 to stop):10 44
Enter coefficient and exponent (0 0 to stop):9 120
Enter coefficient and exponent (0 0 to stop):0 0
DEBUG: before sort
9 8 3 10 6 2 10 44 9 120
3 10 10 44 9 120 9 8 6 21
DEBUG: After sort
It should be 9 120 10 44 3 10 9 8 6 2
counter is the size of the list.
Code:
void LList::sort() {
listNode *temp;
temp = head;
int j;
for(j = 0; j < counter; j++) {
while(temp->next != NULL) {
if(temp->expo < temp->next->expo) {
int tempInt, tempInt2;
tempInt = temp->next->expo;
tempInt2 = temp->next->coef;
temp->next->expo = temp->expo;
temp->next->coef = temp->coef;
temp->expo = tempInt;
temp->coef = tempInt2;
} else {
temp = temp->next;
}
} //end while
} // end for
} // end function
This is the class decleration of the list
class listNode {
public:
int coef;
int expo;
listNode *next;
};
Any help would be great thanks.
Code tagged by Hammer.