I did not get to read the program completely or run it or test it, but my surmise is you are not incrementing left pointer and decrementing right pointer after swapping as I have indicated below.

On 6/25/06, Thomas.Chang <[EMAIL PROTECTED]> wrote:

Following is a qsort() program, I checked it carefully severally times
and tested with a lot of cases without finding any error. But when I
submit the program using it to online judge, it always "wrong answer",
if I substitute it with the standard qsort() of c library, it will
pass.
I post it here wishing someone can point out my mistakes. It's really
frustrating having problem with such basic, simple algorithms.

-----------------------------------------------------------------------------------
int sticks[100];
void sort(int left,int right){
        int pivot, i, j;
        int temp;
        if(left<right){
                i=left; j=right+1;
                pivot = sticks[left];
                do{
                        do i++;
                        while (i<=right && sticks[i] < pivot );
                        do j--;
                        while (j>=left && sticks[j] > pivot);
                        if(i<j && i<=right && j>=left) {
                                temp = sticks[i];
                                sticks[i] = sticks[j];
                                sticks[j] = temp;
                        } // <potentialBug> need an increment/decrement on swap </potentialBug>
                }while(i<j);
                if(j>left){
                        temp = sticks[left];
                        sticks[left] = sticks[j];
                        sticks[j] = temp;
                }
                if(left<j) sort(left,j-1);
                if(j<right && left<=j) sort(j+1, right);
        }
}



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to