Reputation: 11
I tried to translate the Quicksort code from Java to Python but it didnt work. Can someone tell me where the Problem is? I'm getting a "maximum recursion depth exceeded in comparison" but with my example I only want to order less than 10 integers so I dont think this is the real problem...
def help(array, low, high):
pivot = array[low]
fromhigh = high
fromlow = low
while True:
while(array[fromhigh]>pivot):
fromhigh = fromhigh-1
while(array[fromlow]<pivot):
fromlow = fromlow+1
if(fromlow<fromhigh):
array[fromlow], array[fromhigh] = array[fromhigh], array[fromlow]
else:
return fromhigh
def quickSort(array, low, high):
if (low<=high):
pivot = help(array, low, high)
quickSort(array, low, pivot)
quickSort(array, pivot + 1, high)
#Testarray
array = [10, 7, 2 , 8, 9, 1, 5, 11, 13]
n = len(array)
quickSort(array, 0, n-1)
print("Sorted Array:")
for i in range(n):
print("%d" % array[i]),
Upvotes: 1
Views: 36
Reputation: 98
If you add a print(low, high)
at the beginning of your quickSort
function, you'll notice that it prints 0 0
all the time until it crashes.
The if
condition there is incorrect. Instead of low <= high
it should be low < high
, because you don't want to continue sorting a single-element subarray.
Upvotes: 1