-2

I’m trying to implement an in-place Quick Sort in Python. I have two slightly different versions of my partitioning logic, and I’m confused because both seem correct on small arrays, but the second version can cause RecursionError: maximum recursion depth exceeded

Here are the two versions:

def quick_sort_safe(arr, low, high):
    if low < high:
        pivot = arr[high]
        p = low - 1
        for i in range(low, high):
            if arr[i] <= pivot:
                p += 1
                arr[i], arr[p] = arr[p], arr[i]
        arr[high], arr[p+1] = arr[p+1], arr[high]
        pivot_index = p + 1
        quick_sort_safe(arr, low, pivot_index-1)
        quick_sort_safe(arr, pivot_index+1, high)

def quick_sort_overflow(arr, low, high):
    if low < high:
        pivot = arr[high]
        p = low
        for i in range(low, high):
            if arr[i] <= pivot:
                arr[i], arr[p] = arr[p], arr[i]
                p += 1
        arr[high], arr[p] = arr[p], arr[high]
        pivot_index = p
        quick_sort_overflow(arr, low, pivot_index-1)
        quick_sort_overflow(arr, pivot_index+1, high)

Problem description: this is the error i'm getting: ->RecursionError: maximum recursion depth exceeded there is not such error like indexOutOfRange

The difference between this two functions is just how i calculate the position of the pivot, and when i debug it on very big array, the second one give me an recursion error. from what i see they should behave the same way

14
  • 2
    You said "the first version can cause stack overflow on large arrays with many duplicates" at the top. Is this a mistake? You later seem to indicate that version 2 fails sometimes. Commented Nov 3 at 21:12
  • 2
    Welcome to SO! To be frank, we're not here to do your debugging for you. Check out How to debug small programs by Eric Lippert; see also How to step through Python code to help debug issues and minimal reproducible example. What we want to see is that you understand the difference between the two functions, and ask a specific question about that, or at least ask a specific question about why you don't understand the difference (think compare and contrast). You can edit if needed. Commented Nov 3 at 21:15
  • From just glancing at the code, it looks like you have an off-by-one error, though I'm not sure where since as PT said, it's not clear which version works. Commented Nov 3 at 21:18
  • 1
    We're also not going to come up with an example for you; that's on you. Why don't you have an example? Is this homework? If so, check out How to ask and answer homework questions. Commented Nov 3 at 21:21
  • 1
    Consider using the Hoare partition scheme instead. Commented Nov 4 at 6:52

1 Answer 1

1

Not really an answer but a long comment.

Typically its not large or small arrays. As noted in the comments above, its basically the low and high values not reducing by 1 for some situation/input/scenario. In other words when you are invoking the recurision call the same low,high sequence is repeating without reducing by 1 which is ideally the case.

Even with a single pivot, the variation I am familiar with uses mid instead of high. You swap mid with the first then sort range between second till end and swap the meeting point between low and mid with the first position(pivot). Then repeat for range one less than pivot on the left and one more on the right.

Also using recursion for quick sort may itself be a bad design since its n-squared complexity and for scenarios like yours where array can be descending order, you could end up with upto n recursion (stack-overflow).

Your best bet would be to create a sample input set of odd and even length arrays (with unique ones as well as with all elements the same and some variations). Print how the array is being sorted (within the loops and the conditions ) and see if that aligns with your understanding.

Sign up to request clarification or add additional context in comments.

2 Comments

"you could end up with n-squared recursion (stack-overflow)"????? I don't believe any correct implementation of quicksort is capable of having more than N (as in "array length") nested recursive calls... it is quite unclear what your statement means....
My bad. You are correct. I should have said n. Will correct it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.