1

这是我的代码:

from random import randint

def quick_sort(sort_me):
    if len(sort_me) < 2:
        return sort_me

    pivot = sort_me[0]

    this = lower = upper = []

    for x in sort_me:
        if x < pivot:
            lower.append(x)
        elif x > pivot:
            upper.append(x)
        else:
            this.append(x)

    return quick_sort(lower) + this + quick_sort(upper)

我在终端能看到的是:

File "sorts.py", line 19, in quick_sort
  return quick_sort(lower) + this + quick_sort(upper)
RuntimeError: maximum recursion depth exceeded

我认为this列表有问题,但我不知道是什么。帮助!

4

1 回答 1

5
this = lower = upper = []

赋值永远不会在 Python 中创建副本。此行不会创建 3 个列表;它创建 1 个列表,并使this,lowerupperall 引用该列表。列 3 个清单。

this = []
lower = []
upper = []
于 2013-08-04T22:17:46.667 回答