在 Python 快速排序期间计算对列表的总访问量

如何解决在 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。 抱歉,我在代码中犯了一些错误。 我修改了它,仍然得到了有线输出。 我感谢您的任何指导。非常感谢。

解决方法

为了正确计算数组访问,您需要进行的主要更改是:

  1. 保持 count 作为全局变量,以便 inplace_quick_sort() 的每个分支在函数末尾更新相同的计数器。从函数定义和用法中删除 y 并使用 global count 启动主函数。

  2. count += 1 之前的两个 while 应该在每个 while 循环的内部/开始处,因为每个 while 循环都访问 arr[left]arr[right]。因此,该计数器应该为 while

    的每次迭代增加
  3. 对于语句 while left <= right and arr[left] <= pivot,没有必要访问 arr[left] - 如果 left <= right 为 False,则永远不会评估 arr[left] <= pivot,并且 {{1 }} 不被访问。这必须拆分为不同的步骤:

  4. 这一行应该被删除,因为当你调用它时,arr[left] 只会被访问一次。剩下的时间是递归的,所以a在那里更新。

    • coun
  5. 如果数组“访问”只包括读而不包括写,那么两行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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?