Python multithread "maximum recursion depth exceeds"

I use Python multithreading to implement Quicksort. Quicksort is implemented in function. This is a recursive function. Each thread calls Quicksort to sort the array that it has. Each stream has its own array, which stores the numbers that need to be sorted. If the size of the array is smaller (<10,000). It is working fine. However, if the size of the array is larger, it shows "maximum recursion depth". Thus, I use the setrecursionlimit () function to reset the recursion depth to 1500. But the program crashes directly ... Below is a quick sort code. It works well, if not in a multi-threaded environment. It seems that multiple threads are causing the recursion depth problem.

def partition (array, p, r):
    x = array[r]
    i = (p-1)
    j = p
    while (1):
        if array[j] <= x:
            i = (i+1)
            temp = array[j]
            array[j] = array[i]
            array[i] = temp
        j+=1
        if j == r:
            break
    temp = array[i+1]
    array[i+1] = array[r]
    array[r] = temp
    return i+1

def quicksort (array, p, r):
    if p < r:
        q = partition (array, p, r)
        quicksort (array, p, q-1)
        quicksort (array, q+1, r)
+3
5

, : " "? .

-, . , . , , , . " ": .

, . . , . ( ).

/ C, Python (, CPython). Python C, , . Python, setrecursionlimit, .

unix-ish- bash ulimit -s. help ulimit bash .

+4

? ?

, ; . , , scipy numpy.

, , .

+1
  • quicksort. quicksort, .

    Python ( , CPython), . , , . , segfault, . (, , ) , , . , : .

  • ( ), . , . , Python , , . , Python, , . ( , , .)

+1

, , - , , , , , . , - , .

If you really want to implement quicksort for a large number of elements, you will need to read this Wikipedia article about memory usage specifically using quicksort. Otherwise, as Nathan suggested, Python already has a built-in function sorted(). If this is not homework or curiosity, I would highly recommend using this.

0
source

Here is the iterative code for QuickSort

    import time
    import random

    stack = []

    def partition(data,p,q):
        global stack
        pivot = p
        pivotvalue = data[q]
        for index in range(p,q+1):
            if data[index] < pivotvalue:
                temp = data[index]
                data[index] = data[pivot]
                data[pivot] = temp
                pivot = pivot + 1
        temp = data[q]
        data[q] = data[pivot]
        data[pivot] = temp
        return pivot

    def qSort(data,p,q):
        global stack
        push(stack,p,q)
        while isEmpty(stack) == False:
            q = pop(stack)
            p = pop(stack)
            pivot = partition(data,p,q)
            if pivot-1 > p:
                push(stack,p,pivot-1)
            if pivot+1 < q:
                push(stack,pivot+1,q)


    def push(stack,p,q):
        stack.append(p)
        stack.append(q)

    def pop(stack):
        global top
        if(len(stack)==0):
            return -1
        element = stack.pop()
        return element

    def isEmpty(stack):
        return len(stack) == 0

    if __name__ == '__main__':
        start_time = time.time()
        data = (range(1000000,0,-1))
        random.shuffle(data)
        #print data
        qSort(data,0,len(data)-1)
        #print data
        print time.time() - start_time, "seconds"
0
source

All Articles