Python Quicksort Runtime Error: Maximum Recursion Depth Exceeded In Cmp
Solution 1:
You have simply hit the recursion limits. Your list of names is too large for Python's limited recursion capabilities. Your Quicksort works just fine otherwise.
You could raise the recursion limit by setting the limit higher with sys.setrecursionlimit()
. You can set it a fair amount higher, but you do so at your own risk.
A better option is to use the built-in Python sort; the TimSort algorithm is far superior and won't hit a recursion limit:
names = sorted(names, key=len)
This sorts the names by their length, shortest names first.
Solution 2:
You exceed python default recursion size. The default recursion limit is 1000. You can increase recursion limit but it is not recommended. Here is how to do
import sys
sys.setrecursionlimit(1500)
Suggestion My suggestion is use numpy.argsort() method which is already prepared for many sorting algorithms. Here is simple examle for how to do quicksort algorith by using numpy library.
Post a Comment for "Python Quicksort Runtime Error: Maximum Recursion Depth Exceeded In Cmp"