0
import sys
import time
from random import randint
import numpy as np

sys.setrecursionlimit(6000)
nums = [10, 50, 100, 500, 1000, 5000]

def bubble(A, n):
    for i in range(n - 1):
        if A[i] > A[i + 1]:
            A[i], A[i + 1] = A[i + 1], A[i]
    if n - 1 > 1:
        bubble(A, n - 1)

def time_by_bubble_sort(nums):
    time_taken_by_bubble_sort = []
    for num in nums:
        A = list(np.random.randint(low=1, high=num, size=num))
        st_time = time.time()
        bubble(A, len(A))
        end_time = time.time()
        time_taken = end_time - st_time
        time_taken_by_bubble_sort.append(time_taken)
    return time_taken_by_bubble_sort

print(time_by_bubble_sort(nums))

I want to compare time with my values from nums: [10, 50, 100, 500, 1000, 5000] Why dosen't generate a time for the last value (5000), but when I switch it out to 2000 or remove it, it will print?

this is the error code: exit code -1073741571 (0xC00000FD)

After googling, it might be that my recursive function goes to infinite, but I don't see it.

sorry for bad english.

EggCoder
  • 37
  • 5
  • In your own words, where the code says `sys.setrecursionlimit(6000)`, what do you think that means? *Why do you suppose* that it is necessary to do this? Of course, in order to learn that there is such a tool, you must have found out that there is a limit on recursion by default, that would not allow the code to complete - right? So. *Why do you suppose that limit is there*? Do you suppose it might have something to do with the problem you encountered? In the place where you found out that there is such a thing named `sys.setrecursionlimit`, did you try reading the rest of the explanation? – Karl Knechtel Dec 03 '22 at 20:19

1 Answers1

1

According to sys.setrecursionlimit, emphasis mine:

The highest possible limit is platform-dependent. A user may need to set the limit higher when they have a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.

Your limit of 6000 is too high. My system crashes after about 2130 recursive calls to bubble, which explains why using 2000 instead of 5000 works. The crash is due to the Python process's stack running out of space which is platform-dependent.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251