python的QuickSort在大输入中;如何修复错误代码:使用退出代码-1073741571(0xc00000fd)完成的过程?

发布于 2025-01-25 08:33:39 字数 390 浏览 4 评论 0 原文

我在Python中写了递归的QuickSort算法,以对10,000个INT列表进行排序。我正在测试一个最坏的情况(枢轴始终是较低的索引,并且列表已经对其进行排序,因此对QuickSort的递归调用始终为空,另一个始终降低1)。我知道这意味着我必须使用

sys.setRecursionLimit(10000)

现在增加错误代码

处理带有退出代码-1073741571(0xc00000fd)

,我应该使用

threeing.stack_size()

,但是,我不知道如何使用此功能。我尝试传递不同的值,并且要么要么明显大小无效,要么是相同的错误代码。有人可以帮我吗?

I wrote a recursive quicksort algorithm in Python to sort a list of 10,000 ints. I am testing a worst case (pivot is always the low index, and the list is already sorted, so one of the recursive calls to quicksort is always empty, and the other always decreases by 1). I know that means I have to increase the recursion limit with

sys.setrecursionlimit(10000)

Now I am getting the error code

Process finished with exit code -1073741571 (0xC00000FD)

I read elsewhere that I should use

threading.stack_size()

But, I have no idea how to use this function. I tried passing in different values, and I will either get that the size is not valid, or the same error code. Can somebody please help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-02-01 08:33:39

通过在较小的分区上递归并在较大的分区上循环,可以将堆栈空间限制为O(log(n))。最坏的情况时间复杂性仍然是O(n^2)。

def qsort(a, lo, hi):
    while(lo < hi):
        p = a[(lo + hi) // 2]       # pivot, any a[] except a[hi]
        i = lo - 1
        j = hi + 1
        while(1):                   # Hoare partition
            while(1):               #  while(a[++i] < p)
                i += 1
                if(a[i] >= p):
                    break
            while(1):               #  while(a[--j] < p)
                j -= 1
                if(a[j] <= p):
                    break
            if(i >= j):             #  if indexes met or crossed, break
                break
            a[i],a[j] = a[j],a[i]   #  else swap elements and loop
        if((j - lo) < (hi - j)):    # recurse on smaller partition
            qsort(a, lo, j)         #  loop on larger partition
            lo = j+1
        else:
            qsort(a, j+1, hi)
            hi = j

Stack space can be limited to O(log(n)) by recursing on smaller partition, and looping on larger partition. Worst case time complexity is still O(n^2).

def qsort(a, lo, hi):
    while(lo < hi):
        p = a[(lo + hi) // 2]       # pivot, any a[] except a[hi]
        i = lo - 1
        j = hi + 1
        while(1):                   # Hoare partition
            while(1):               #  while(a[++i] < p)
                i += 1
                if(a[i] >= p):
                    break
            while(1):               #  while(a[--j] < p)
                j -= 1
                if(a[j] <= p):
                    break
            if(i >= j):             #  if indexes met or crossed, break
                break
            a[i],a[j] = a[j],a[i]   #  else swap elements and loop
        if((j - lo) < (hi - j)):    # recurse on smaller partition
            qsort(a, lo, j)         #  loop on larger partition
            lo = j+1
        else:
            qsort(a, j+1, hi)
            hi = j
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文