Pointer problems with bubble sort

This is a discussion on Pointer problems with bubble sort within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a typical function to sort an unordered list using the bubble sort method. The problem ...

  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,299
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 12: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, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21