Reputation: 29
I've got a problem while i was implementing a Quick sort algorithm, just like in this case, python interpretor automaticlly restarting without returning answer . I couldn't find a more similar question than this one. As this post's been inactive since 2012 and the problem wasn't solved i decided to ask again. Here goes my code:
#coding:utf8
import time
import sys
sys.setrecursionlimit(1000000)
x=eval(input("Type a list: "))
start=time.time()
L=[]
R=[]
L_=[]
R_=[]
c=0
z=x[0]
for j in range(1,len(x)):
if x[j]>= z:
R.append(x[j])
elif x[j] <= z:
L.append(x[j])
def fun(x,lista,c):
for i in range(len(x)-1):
for j in range(i+1,i+2):
if x[i]<=x[j] and c!=len(x)-2:
c+=1
elif x[i]>x[j]:
lista.append(x[i])
lista.append(x[j])
x[i]=lista[1]
x[j]=lista[0]
lista=[]
c=0
return fun(x,lista,c)
elif x[i]<=x[j] and c==len(x)-2:
c=0
return x
fun(L,L_,c)
fun(R,R_,c)
print(L+[z]+R)
end=time.time()
print(end-start)
The Python's version that i'm using is 3.4. As you can see, i used the sys
library to increase the recursion limit, but it didn't solve my problem. I tested it, and it can sort an 'X' amount of elements that is in the range of 300>X>250, if i'm right. How can I solve this problem? Thank you, in advance.
Upvotes: 0
Views: 9152
Reputation: 9
i was just about to ask the same question, and so suscrbied to stackoverflow.
My Python was restarting during long python programs, more specifically mostly during programs calling external programs (for exemple during calls to FFMPEG or calls to Tesseract with system.os command).
My searches had led me to 4 questions on stackoverflow, and i learned what follow :
Python IDLE run user code in a subprocess, and RESTART occurs if this subprocess crashes. (Thanks Terry Jan Reedy).
I had the beliefs that it was my problem (subprocess crashes).
At first, i though it was an Hardware problem : I tested my CPU, checked my HDD, tested my RAM with Memtest ... And got no error detected.
Then i runned my programs on my second computer, which is exactly the same as my first one : everything was ok on the second computer, but not on my first one.
After that i ran a SystemFileCheck (i was using Windows), and saw that my OS had problem that it couldn't resolve.
Finally i deleted the OS partition and reinstalled Windows.
Now everything is perfectly fine. --> So my Windows was killing my python process whithout reason.
Conclusion :
Fix : Check your program for bugs, and repare or reinstall your OS.
Upvotes: 0
Reputation: 19184
Idle normally (unless started with -n) runs user code in a user subprocess. The RESTART line means that the subprocess has been restarted. Regardless of how one starts Idle (without -n) this occurs either when one selects Shell -> Restart Shell Cntl+F6 or Run -> Run Module F5.
RESTART also occurs if the user process crashes from a program bug. If one starts Idle from a command line (python -m idlelib
, or ... idlelib.idle
in 2.x, an error message might appear there, but this does not happen with your program.
I ran your program with 3.4.3 after first replacing the input line with a concrete assignment (which you should have done before posting)
x = list(reversed(range(1000)))
(This particular choice is a worst-case for many sorting algorithms).
I did not see anything in the command window but did see the Windows message that python.exe had stopped working. Since Idle was still running, this referred to the error process.
Reducing 1000000 to 10000 did not change anything. Leaving the recursion limit at 1000 lead to a long traceback.
Traceback (most recent call last):
File "C:\Programs\python34\tem.py", line 33, in <module>
fun(L,L_,c)
File "C:\Programs\python34\tem.py", line 29, in fun
return fun(x,lista,c)
....
File "C:\Programs\python34\tem.py", line 29, in fun
return fun(x,lista,c)
File "C:\Programs\python34\tem.py", line 18, in fun
for i in range(len(x)-1):
RuntimeError: maximum recursion depth exceeded in comparison
Reducing the list to size 100 did not change the outcome. You have an infinite loop, which you need to prevent.
You code does not look like the versions of quicksort I am familiar with. Perhaps you should review the definition of the algorithm.
However, this line
for j in range(i+1,i+2):
looks like a bug as it iterates just once, with j = i+1
. As it is, the program runs the same if you replace it with that line and dedent the following lines.
In any case, the terminating condition x[i]<=x[j] and c==len(x)-2
is not being met. I suggest starting with a short list with just a few items and add from print statements to fun
to see how values deviate from what you expect. You could also try executing your algorithm by hand.
Upvotes: 0