I use Python multithread to realize Quicksort. Quicksort is implement in a function. It is a recursive function. Each thread calls Quicksort to sort the array it has. Each thread has its own array that stores the numbers needs to be sorted. If the array size is smaller (<10,000). It runs ok. However, if the array size is larger, it shows the "maximum recursion depth exceed". So, I use setrecursionlimit () function to reset the recursion depth to 1500. But the program crash directly... The following is quicksort code. It works well if not in multi-thread environment. It seems multiple threads is the cause of 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)