如何解决在 Python 快速排序期间计算对列表的总访问量
我的任务是通过 Python 计算快速排序程序中的列表访问总数。 请检查以下代码:
const dirHandle = await window.showDirectoryPicker();
const fileHandle = await dirHandle.getFileHandle(entry.name,{});
const file = await fileHandle.getFile();
我有两个问题。 第一个是“计数”正确添加列表访问的数字? 第二个是我得到了如下所示的有线输出:
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,left - 1,count)
inplace_quick_sort(arr,left + 1,count)
x = 0
print("count = " + str(inplace_quick_sort(arr1,len(arr1) - 1,x)))
我不明白关于“计数”的迭代。为什么“计数”等于8? “计数”假设大于 8。 抱歉,我在代码中犯了一些错误。 我修改了它,仍然得到了有线输出。 我感谢您的任何指导。非常感谢。
解决方法
为了正确计算数组访问,您需要进行的主要更改是:
-
保持
count
作为全局变量,以便inplace_quick_sort()
的每个分支在函数末尾更新相同的计数器。从函数定义和用法中删除y
并使用global count
启动主函数。 -
的每次迭代增加count += 1
之前的两个while
应该在每个while
循环的内部/开始处,因为每个 while 循环都访问arr[left]
或arr[right]
。因此,该计数器应该为while
-
对于语句
while left <= right and arr[left] <= pivot
,没有必要访问arr[left]
- 如果left <= right
为 False,则永远不会评估arr[left] <= pivot
,并且 {{1 }} 不被访问。这必须拆分为不同的步骤: -
这一行应该被删除,因为当你调用它时,
arr[left]
只会被访问一次。剩下的时间是递归的,所以a
在那里更新。coun
-
如果数组“访问”只包括读而不包括写,那么两行
count += 1 # for the access to the "a" element in the list while calling the function
应该只是count += 4
。我已按照您的代码保留它,请相应更改或保持原样。
count += 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,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,len(arr1) - 1)
print("count = ",count)
print('array afer:',arr1)
顺便说一句,如果您确实想使用 count = 30
array afer: [2,4,7]
作为局部变量,那么:
- 应用上述更改,但跳过 #1。
-
count
语句应为if a >= b: return
- 每次调用
if a >= b: return count
都应该增加前一个inplace_quick_sort
并确保最后count
:-
return count
-
此外,这个答案只是正确count = inplace_quick_sort(arr,left - 1,count)
count = inplace_quick_sort(arr,b,count)
return count
,没有像完成那样修复快速排序的实现。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。