Python Quicksort Runtime Error: Maximum Recursion Depth Exceeded in cmp

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.

Leave a Comment