Reputation: 9
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)
Upvotes: 0
Views: 48
Reputation: 33509
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
Upvotes: 1