0

So I wrote a QuickSort Algorithm in Python and I have been getting this error (below)

Traceback (most recent call last):
File "hw2.py", line 5, in print
print(input[i])
File "hw2", line 5, in print
print(input[i])
File "hw2", line 5, in print
print(input[i])
[Previous line repeated 996 more times]
File "hw2", line 4, in print
  for i in range(0, INPUT_SIZE):
RecursionError: maximum recursion depth exceeded in comparison

Any idea what is wrong with it? I already added a recursion limit to my code but it didn't do anything(code below) Any help would be appreciated :)

import sys
sys.setrecursionlimit(15000)

def print(input):
    for i in range(0, INPUT_SIZE):
        print(input[i])
    print("\n")

def partition(input, p, r):
    pivot = input[r]

    while(p < r):
        while(input[p] < pivot):
            p = p + 1
        while(input[r] > pivot):
            r = r - 1
        if(input[p] == input[r]):
            p = p+1
        elif(p < r):
            tmp = input[p]
            input[p] = input[r]
            input[r] = tmp
    return r

def quicksort(input,p,r):
    if(p<r):
        j = partition(input,p,r)
        quicksort(input,j-1)
        quicksort(input,j+1,r)



if __name__ == '__main__':
    INPUT_SIZE = 10
    input = [500,700,800,100,300,200,900,400,1000,600]
    print("Input: ")
    print(input)
    quicksort(input, 0, 9)
    print("Output: ")
    print(input)
2
  • Having to call sys.setrecursionlimit is usually a code smell in itself. Your Quicksort code is just buggy, plain and simple, with unbounded recursion. Commented Apr 9, 2020 at 18:00
  • Try to avoid naming your variables and functions after the built-in functions of Python. Commented Apr 9, 2020 at 18:28

1 Answer 1

1

You define a function print, which calls print (i.e. itself).

This results in an infinite recursion.

Change the name of the function to something else, e.g. display

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.