×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Apr 2005
    Location
    England
    Posts
    91

    Unhappy Bubble sort a list of linked objects!

    Hello all,
    Ive been working for hours and days at this, so if you have any idea what im talking about, please help!

    Overview: I have a number of objects of type CList which are linked together using pointers (like a linked list but with objects rather than structs). I need to sort the list by quantity .

    What Ive tried: Putting the pointers to the objects in a RPointerArray. Trying to bubble sort this. Then casting this back to the original pointers which join the objects! Sounds complex i know, but ive tried all the easy options and its just evolved to this.

    What i have so far...
    General Initilasation where pointers is the RPointerArray
    Code:
    	TInt j, k;
    	CList* probe;
    	TBool exchange_made = ETrue;
    
    	// Initialize pointer array
    	probe = Head;
    	for (k = 0; k < (NodeCount + 1); k++)
    		{
    		pointers.Insert(probe, k);
    		probe = probe->Next;
    		}
    
    	k = 0;
    Then the sort itself
    Code:
    while ((k < NodeCount) && exchange_made)
    {
    	exchange_made = EFalse;
    	k++;
    	for (j = 0; j < (NodeCount+1) - k; j++)
    		if (pointers[j]->Quantity > pointers[j + 1]->Quantity)
    		{
    
    		CList* temp = static_cast<CList*> (pointers[j]);	     // Swap pointers
    		CList* temp2 = static_cast<CList*> (pointers[j+1]);
    				
    		pointers.Remove( j );
    				User::LeaveIfError( pointers.Insert(temp2, j)  );
    				
    	pointers.Remove( j+1 );
    				User::LeaveIfError( pointers.Insert( temp, j+1)  );	
    				exchange_made = ETrue;
    	delete temp;
    	delete temp2;
    	}
    }
    The assigning the list back to the original pointers

    Code:
    TInt ptrCount = pointers.Count();
    		
    Head = static_cast<CList*> (pointers[0]);
    Head->Next = static_cast<CList*> (pointers[1]);
    		
    CurrentPtr = Head->Next;
    		
    for (TInt i = 1; i < ptrCount-1; i++)
    	{
    	CurrentPtr->Next = static_cast<CList*> (pointers[i]);
    	CurrentPtr = CurrentPtr->Next;
    	}
    I think the problems somewhere within the bubble sort bit itself, and moving the pointers arround. If you can see fault with it please tell me, or if you know of a better way to sort the pointers around, please let me know. (and ive tried not using the pointerarray and assigning CurrentPtr->Next = temp, but that didnt seem to work!)

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    22
    I think you shouldn't at least delete temp and temp2 in the sorting loop.

  3. #3
    Regular Contributor
    Join Date
    Apr 2005
    Location
    England
    Posts
    91
    delete temp and temp2 was put there by error from a previous solution as in this instance they are not assigned any memory and would be deleted when the go out of scope anyway.

    Ive also missed out two other lines on this version; a temp3 variable where the previous pointer was pointing to and assigning this to temp2 if the swap is made.

    Ive stepped through the code while debuggin, noting the addresses of the pointers, but the swap doesnt seem to be made, even though that line of code is executed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •