Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,569 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,917 people online right now. Registration is fast and FREE... Join Now!




Quicksort problems

 
Closed TopicStart new topic

Quicksort problems, goes into an infinite loop

ibaraku
10 Apr, 2008 - 06:12 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
This quicksort keeps going into an infinite loop, I cannot find the problem, can anybody help me??? thanks!!!
CODE

template <typename ItemType, typename ADT>
void Sort<ItemType,ADT>::quickSort(void)
{
//  implement this function!
//
//  call internal function:
    _quickSort(items.begin(), --items.end());
}

template <typename ItemType, typename ADT>
void Sort<ItemType,ADT>::_quickSort(typename ADT::iterator first, typename ADT::iterator last)
{
    typename ADT::iterator pivot, left_side, right_side;
    pivot=first;//will always set our pivot equal to first item!!!
    left_side=last;//lesser number come left to the pivot
    right_side=++first;//greater number come right of the pivot
    --first;//recover actual value of first
    cout<<__LINE__<<"Before while"<<endl;
    while(*left_side<*right_side)
    {
        cout<<__LINE__<<"Inside while"<<endl;
        cout<<__LINE__<<"left_side"<<*left_side<<endl;
        cout<<__LINE__<<"right_side"<<*right_side<<endl;
        --left_side;
        while(*left_side < *pivot);
        cout<<__LINE__<<"Inside 2nd while"<<endl;
        while(right_side != items.begin() && *--right_side > *pivot);
        cout<<__LINE__<<"inside 3rd while"<<endl;
        if(*left_side < *right_side)
        {
            swap(*left_side, *right_side);
        }//end if
    }//end while
    swap(*left_side, *first);
    _quickSort(first, --left_side);
    _quickSort(++left_side, last);

}//end _quickSort

User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/2/08 11:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month