[英]Bug in my quick sort algorithm implementation
Recently I was trying to implement the quicksort algorithm in Python, and I couldn't make it work properly. 最近,我试图在Python中实现quicksort算法,但无法使其正常运行。 Even though the program sorts the sub-arrays, it's not being reflected on the main list. 即使程序对子数组进行排序,也不会反映在主列表中。 I'm new to programming, so can anyone help me to understand what part or concept I didn't do right? 我是编程新手,所以有人可以帮助我了解我做错了哪些部分或概念吗?
def swap(arr, right, left):
temp = arr[right]
arr[right] = arr[left]
arr[left] = temp
def split_array(arr, right):
left_half = arr[:right]
right_half = arr[right:]
a = (left_half, right_half)
return a
def quick_sort(arr):
if len(arr) >= 2:
pivot = 0
left_mark = pivot + 1
right_mark = len(arr) - 1
stop = True
while stop:
while arr[pivot] > arr[left_mark] and left_mark < right_mark:
left_mark += 1
while arr[pivot] < arr[right_mark] and left_mark < right_mark:
right_mark -= 1
if left_mark < right_mark:
swap(arr, right_mark, left_mark)
right_mark -= 1
left_mark += 1
else:
if arr[pivot] > arr[right_mark]:
swap(arr, right_mark, pivot)
stop = False
left, right = split_array(arr, right_mark)
quick_sort(left)
quick_sort(right)
return arr
array = [8, 6, 1, 7, 0, 5, 4, 3, 2, 1]
print(quick_sort(array))
Change this: 更改此:
left, right = split_array(arr, right_mark)
quick_sort(left)
quick_sort(right)
to this: 对此:
left, right = split_array(arr, right_mark)
arr = quick_sort(left) + quick_sort(right)
Quicksort is typically implemented "in-place" to avoid copying arrays around. Quicksort通常是“就地”实现的,以避免在周围复制阵列。 Your implementation creates a full copy of the array at each step instead and returns it, so you need to reassemble the pieces yourself. 您的实现会在每个步骤中创建数组的完整副本并返回它,因此您需要自己重新组装各个部分。
UPDATE 更新
A small change to make your algorithm in-place instead: 进行一下小的更改以使您的算法就位:
def quick_sort(arr, start=0, end=None):
if end is None: end = len(arr)-1
if end > start:
pivot = start
left_mark = start + 1
right_mark = end
stop = True
while stop:
while arr[pivot] > arr[left_mark] and left_mark < right_mark:
left_mark += 1
while arr[pivot] < arr[right_mark] and left_mark < right_mark:
right_mark -= 1
if left_mark < right_mark:
swap(arr, right_mark, left_mark)
right_mark -= 1
left_mark += 1
else:
if arr[pivot] > arr[right_mark]:
swap(arr, right_mark, pivot)
stop = False
quick_sort(arr, start, right_mark - 1)
quick_sort(arr, right_mark, end)
array = [8, 6, 1, 7, 0, 5, 4, 3, 2, 1]
quick_sort(array) # in-place
print(array) # now sorted
IMO, the following is clearer and more closely matches the typical description of the algorithm: IMO,以下内容更加清楚,并且与算法的典型描述更加匹配:
def quick_sort(arr, start=0, end=None):
if end is None:
end = len(arr) - 1
if end <= start:
return
pivot = arr[start]
left_mark = start - 1
right_mark = end + 1
while left_mark < right_mark:
left_mark += 1
while arr[left_mark] < pivot:
left_mark += 1
right_mark -= 1
while arr[right_mark] > pivot:
right_mark -= 1
if left_mark < right_mark:
arr[left_mark], arr[right_mark] = arr[right_mark], arr[left_mark]
quick_sort(arr, start, right_mark)
quick_sort(arr, right_mark + 1, end)
You are exactly right! 你说得对! left
and right
are separate entities after split_array
, not parts of the original arr
. left
和right
是split_array
之后的单独实体,不是原始arr
。 After quick_sort(right)
, say arr=left+right
. 在quick_sort(right)
,说arr=left+right
。 I think that will do it. 我认为这可以做到。 (Need to check the case when there is only one element in either left
or right
.) (需要检查left
或right
只有一个元素时的情况。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.