快速排序递归计数

发布于 2025-02-06 07:04:48 字数 68 浏览 1 评论 0原文

使用快速排序对n个元素进行排序列表的最小递归调用数量是多少。 我不明白,实际调用了递归函数的次数,特别是“最低数字”的含义

What is the minimum number of recursive call to sort a list of n elements using quick sort.
I cannot understand that how many times the recursion function is actually called and specifically what is meant by "minimum number "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2025-02-13 07:04:48

快速排序是算法的集合,其中通过选择枢轴值,分区数据并在分区上进行递归来对数据集进行排序,直到分区大小小于2

。数据分为2集:

  • 比较较小或等于枢轴的元素
  • 比较更大或等于枢轴

有效实现的元素使用详尽的方法选择枢轴值并将数据分配为3组:

  • 比较小于小于小于小于枢轴
  • 比较枢轴的元素比较大于枢轴
  • 的元素

,它们也可能切换到在特定分区大小以下的不同算法和检测到病理分布以下的其他算法,以避免二次时间复杂性。

对于三组实现,最佳情况是所有元素比较相等且无需递归的情况。这构成了最小递归调用的数量:0

在其他情况下,递归调用的数量高度取决于数据分布,枢轴选择方法和其他实现选择,例如:

  • 基本情况处理:在递归或输入功能之前测试分区长度,
  • 切换到其他算法,这样作为用于病理分布的小分区或外壳排序的插入排序,

平均而言,快速排序的递归调用数量大约是:

  • 2n ,如果长度测试仅在函数开始时
  • n <> n <> n < /strong>如果在递归 n/t 之前执行测试
  • ,如果切换到另一种算法以低于 t 的阈值的分区长度。

请注意,递归深度(这是一个不同但重要的问题)可以限于 log 2 (n),通过结合迭代和递归,在较小的分区上递归,迭代较大的一个。

还要注意,可以使用长度 log 2 (n)的小数组来跟踪待处理的分区。

Quick Sort is a collection of algorithms where a data set is sorted by choosing a pivot value, partitioning the data and recursing on the partitions until the partition size is smaller than 2.

Simple implementations use the first, last or middle element as pivot and partition the data into 2 sets:

  • the elements that compare less or equal to the pivot
  • the elements that compare greater or equal to the pivot

Efficient implementations use elaborate methods to choose the pivot value and partition the data into 3 sets:

  • the elements that compare less than the pivot
  • the elements that compare equal to the pivot
  • the elements that compare greater than the pivot

They might also switch to a different algorithm below a certain partition size and or if a pathological distribution is detected, to avoid quadratic time complexity.

For the 3 sets implementations, the optimal cases are those where all elements compare equal and no recursion is needed. This constitutes the minimum number of recursive calls: 0.

In other cases, the number of recursive calls is highly dependent on the data distribution, the pivot selection method and other implementation choices such as:

  • base case handling: testing the partition length before recursing or upon entering the function,
  • switching to a different algorithm such as insertion sort for small partitions or shell sort for pathological distributions

On average the number of recursive calls for quick sort is approximately:

  • 2n if the length test is only at the start of the function
  • n if the test is performed before recursing
  • n/t if switching to another algorithm for partition lengths below a threshold of t.

Note that the depth of recursion, which is a different but important question, can be limited to log2(n) by combining iteration and recursion, recursing on the smaller partition and iterating on the larger one.

Note also that quick sort can be implemented without recursion, using small arrays of length log2(n) to keep track of pending partitions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文