Homework/HW02/List.cc
void List::bubbleSort()
{
    if (head) // if the list is not empty
    {
        bool ordered = false;
        ListIterator iter1, iter2; // need two iterators
        while (!ordered) // while the list is not sorted
        {
            ordered = true; // we'll assume it is sorted
            iter1 = head;   // start form the head with one
            iter2 = head->next; // and the next with the other
            while (iter2) 
            {
                if (*iter1 > *iter2) // if two nodes are out of order
                {
                    iter1.swap(iter2); // swap their datums
                    ordered = false;   // and set the flag
                }
                ++iter1; // move both iterators at the same time
                ++iter2; // so they will allways be adjacent nodes
            }
        }
    }
}
// The bubble sort using list iterators.
The bubble sort using list iterators.
The bubble sort using list iterators .
void List::bubbleSort()
{
    if (head) 
    {
        bool ordered = false;
        ListIterator iter1, iter2; 
        while (!ordered) 
        {
            ordered = true; 
            iter1 = head;   
            iter2 = head->next; 
            while (iter2) 
            {
                if (*iter1 > *iter2) 
                {
                    iter1.swap(iter2); 
                    ordered = false;   
                }
                ++iter1; 
                ++iter2; 
            }
        }
    }
}
void List :: bubbleSort ( ) { if ( head ) { bool ordered = false ; ListIterator iter1 , iter2 ; while ( ! ordered ) { ordered = true ; iter1 = head ; iter2 = head -> next ; while ( iter2 ) { if ( * iter1 > * iter2 ) { iter1 . swap ( iter2 ) ; ordered = false ; } ++ iter1 ; ++ iter2 ; } } } }
The bubble sort using list iterators.
The bubble sort using list iterators .
// if the list is not empty // need two iterators // while the list is not sorted // we'll assume it is sorted // start form the head with one // and the next with the other // if two nodes are out of order // swap their datums // and set the flag // move both iterators at the same time // so they will allways be adjacent nodes