python - 通过 Python 计算快速排序期间对列表的总访问量
问题描述
我的任务是通过 Python 计算快速排序程序中的列表访问总数。请检查以下代码:
arr1 = [4, 5, 3, 7, 2]
def inplace_quick_sort(arr, a, b, y):
count = y
count += 1 # for the access to the "a" element in the list while calling the function
if a >= b:
return
count += 1 # access for arr[b]
pivot = arr[b]
left = a
right = b - 1
while left <= right:
count += 1 # access for arr[left]
while left <= right and arr[left] <= pivot:
left += 1
count += 1 # access for arr[right]
while left <= right and pivot < arr[right]:
right -= 1
if left <= right:
count += 4 # access for swap left and right
arr[left], arr[right] = arr[right], arr[left]
left, right = left + 1, right - 1
count += 4 # access for swap left and last
print(count)
arr[left], arr[b] = arr[b], arr[left]
inplace_quick_sort(arr, a, left - 1, count)
inplace_quick_sort(arr, left + 1, b, count)
x = 0
print("count = " + str(inplace_quick_sort(arr1, 0, len(arr1) - 1, x)))
我有两个问题。第一个是“计数”正确添加列表访问的数字?第二个是我得到如下有线输出:
count = 8
我不明白关于“计数”的迭代。为什么“计数”等于 8?“计数”假设大于 8。抱歉,我在代码中犯了一些错误。我修改了它,仍然得到有线输出。我很感激你的任何指导。非常感谢。
解决方案
正确计算数组访问次数所需的主要更改是:
保留
count
为全局变量,以便inplace_quick_sort()
函数末尾的每个分支都更新相同的计数器。y
从函数定义和用法中删除,并以global count
.count += 1
之前的两个while
应该就在每个循环的内部/开始处,while
因为每个 while 循环都在访问arr[left]
或arr[right]
。因此,该计数器应在每次迭代时递增while
对于 statements
while left <= right and arr[left] <= pivot
,没有必要arr[left]
访问 - 如果left <= right
为 False,则arr[left] <= pivot
永远不会评估,arr[left]
也不会访问。这必须分成不同的步骤:该行应该被删除,因为
a
当你调用它时它只会被访问一次。剩下的时间,它是递归的,所以更新coun
t 那里。count += 1 # for the access to the "a" element in the list while calling the function
如果数组“访问”只包括读取而不包括写入,那么这两
count += 4
行应该是 justcount += 2
。我已按照您的代码保留它,相应地对其进行更改或保持原样。
def inplace_quick_sort(arr, a, b):
global count
if a >= b:
return
count += 1 # access for arr[b]
pivot = arr[b]
left = a
right = b - 1
while left <= right:
while left <= right:
count += 1 # access for arr[left]
if arr[left] <= pivot:
left += 1
else:
break # needed to match the original while-logic
while left <= right:
count += 1 # access for arr[right]
if pivot < arr[right]:
right -= 1
else:
break # needed to match the original while-logic
if left <= right:
count += 4 # access for swap left and right
arr[left], arr[right] = arr[right], arr[left]
left, right = left + 1, right - 1
count += 4 # access for swap left and last
# print(count)
arr[left], arr[b] = arr[b], arr[left]
inplace_quick_sort(arr, a, left - 1)
inplace_quick_sort(arr, left + 1, b)
执行:
arr1 = [4, 5, 3, 7, 2]
count = 1 # because you sart with `len(arr1)`
inplace_quick_sort(arr1, 0, len(arr1) - 1)
print("count = ", count)
print('array afer:', arr1)
输出:
count = 30
array afer: [2, 3, 4, 5, 7]
顺便说一句,如果您确实想count
用作局部变量,那么:
- 应用上述更改,但跳过 #1。
if a >= b: return
声明应该是if a >= b: return count
- 每次调用都
inplace_quick_sort
应该增加前一个count
,并确保return count
在最后:-
count = inplace_quick_sort(arr, a, left - 1, count) count = inplace_quick_sort(arr, left + 1, b, count) return count
-
此外,这个答案只是count
正确的,而不是修复快速排序的实现。
推荐阅读
- c++ - C ++中连续行之间的区别
- c++ - 将对向量的元素与字符串c ++进行比较
- winsock - 在使用 recv() 之前检查数据是否可用
- javascript - TypeError:this.state.posts.map 不是函数。我正在使用 rest api 并尝试使用 axios 在 react.js 应用程序中获取数据
- .net - 结合 .net 和 powershell 正则表达式捕获组语法
- node.js - 如何使用 keycloak 对 loopback 4 应用程序进行身份验证
- hadoop - 如何从 SAP HANA 智能数据访问生成的 Apache Drill 中的查询中删除双引号?
- postgresql - 在 docker-compose 中为 postgres 的 initdb 使用环境变量
- python - 将日期时间对象列表转换为字符串
- javascript - 如何更改对象数组中的值