Thread: Pointer problems with bubble sort

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Pointer problems with bubble sort

    I am trying to create a typical function to sort an unordered list using the bubble sort method. The problem I am having is that the values are not being swapped. I guess it's a problem with pointers, but I can't seem to see why.

    Code:
    void UnorderedList::bubbleSort() {
    
        if(size() <= 1)
            throw ListTooShortException();
    
        for(Node* track = getHead()->getNext(); track != NULL; track = track->getNext())
    
            while(track->getElement() < track->getPrevious()->getElement())
                swapNodes(track->getPrevious(), track);
    
    
    }
    
    void UnorderedList::swapNodes(Node*& first, Node*& second) {
        Node* temp = first;
        first = second;
        second = temp;
    }
    The test list I keep using is just 1 2 4 3. I put in some cout lines and it appears that the pointers are all pointing to the right address when they need to. But afterwards I will call a print function and the list still unsorted. Is it because I have multiple pointers on one address so I need to change them all? Is there anything anyone can see wrong with the code?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What is the definition of getPrevious?

    Please make sure you are compiling at the highest warning level and pay attention to all of the warnings. Post any warnings you do not understand.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what about head pointer? I do not see where it is updated when 1st node is swaped with other node...

    also - after you have swapped a pointers... next and previous pointers are still pointing to the same nodes as before...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort directional problems
    By budala in forum C Programming
    Replies: 1
    Last Post: 01-21-2010, 01:33 PM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM