bubble sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackx
    New Member
    • Sep 2007
    • 31

    #1

    bubble sort

    related to my earlier post, I have a linked list of polynomials and I am trying to do a bubble sort to arrange the polynomials in descending order based on the degree.

    Here's the code I have:

    Code:
    void Polynomial::orderDescending(){
    	Polynomial newPoly;
    	newPoly.head = this->head;
    
    	Term * temp1 = this->head;
    	Term * temp2 = temp1->next;
    
    
    	for (int pass = 1; pass < this->numTerms; pass++) {
    		for (int count = 0; count < this->numTerms-1; count++) {
    			if (temp1->deg < temp2->deg) {
    				temp1->next = temp2->next;
    				temp2->next = temp1;
    				
    				if (count = 0) {
    					this->head = temp2;
    				}
    
    				temp2 = temp1->next;
    			}
    			else{
    				temp1 = temp1->next;
    				temp2 = temp2->next;
    			}
    		}
    
    		temp1 = this->head;
    		temp2 = temp1->next;
    	}
    }
    the program ran into a run-time error because the code didn't allow me to reset the temp pointer's head into the (now) swapped linked list in the outer loop.

    Specifically, line 16 didn't seem to change the linked list which I wanted to implement in line 27.

    For example I have the linked list (the data denotes the degree I want to arrange):

    4 -> 5 -> 7

    After the first inner loop ends, I want the linked list to be changed to

    5 -> 7 -> 4.

    This doesn't happen in the code.

    Instead the list became
    7->4 only.

    Can anyone check how to correct my code?
    Thanks
  • blackx
    New Member
    • Sep 2007
    • 31

    #2
    Originally posted by blackx
    related to my earlier post, I have a linked list of polynomials and I am trying to do a bubble sort to arrange the polynomials in descending order based on the degree.

    Here's the code I have:

    Code:
    void Polynomial::orderDescending(){
    	Polynomial newPoly;
    	newPoly.head = this->head;
    
    	Term * temp1 = this->head;
    	Term * temp2 = temp1->next;
    
    
    	for (int pass = 1; pass < this->numTerms; pass++) {
    		for (int count = 0; count < this->numTerms-1; count++) {
    			if (temp1->deg < temp2->deg) {
    				temp1->next = temp2->next;
    				temp2->next = temp1;
    				
    				if (count = 0) {
    					this->head = temp2;
    				}
    
    				temp2 = temp1->next;
    			}
    			else{
    				temp1 = temp1->next;
    				temp2 = temp2->next;
    			}
    		}
    
    		temp1 = this->head;
    		temp2 = temp1->next;
    	}
    }
    the program ran into a run-time error because the code didn't allow me to reset the temp pointer's head into the (now) swapped linked list in the outer loop.

    Specifically, line 16 didn't seem to change the linked list which I wanted to implement in line 27.

    For example I have the linked list (the data denotes the degree I want to arrange):

    4 -> 5 -> 7

    After the first inner loop ends, I want the linked list to be changed to

    5 -> 7 -> 4.

    This doesn't happen in the code.

    Instead the list became
    7->4 only.

    Can anyone check how to correct my code?
    Thanks
    I got my code to work now, hooray!

    Comment

    Working...