简体   繁体   English

排序算法的临界值是多少?

[英]what is cut off value in sorting algorithms?

I was asked to do the quicksort, cut off with insertion sort. 我被要求做快速排序,以插入排序终止。 But I did not understand the meaning of cut off value. 但是我不明白临界值的含义。 I need someone to elaborate this concept with real-world examples. 我需要有人通过实际示例来详细说明这个概念。

It is hard to be sure without seeing the exact statement of requirements. 没有看到确切的要求说明就很难确定。

However, I think that this means that you should use insertion sort ( O(N^2) ) below a certain size (the "cut off") and quicksort ( O(NlogN) ) above that size. 但是,我认为这意味着您应该使用小于特定大小(“截断”)的插入排序( O(N^2) )和大于该大小的O(NlogN)排序( O(NlogN) )。

  • They could mean that you do this test on the size of the input array. 它们可能意味着您需要对输入数组的大小进行此测试。

  • They could also mean that you implement a hybrid sort, and use insertion sort on a subarray when a quicksort partition size is less than the threshold. 它们还可能意味着您实现了混合排序,并在快速排序分区大小小于阈值时在子数组上使用插入排序。

Either interpretation is plausible. 两种解释都是合理的。


I need someone to elaborate this concept with real-world examples. 我需要有人通过实际示例来详细说明这个概念。

I doubt that you need examples to understand this. 我怀疑您需要一些示例来理解这一点。 If you understand the insertion sort and quicksort algorithms, then this is conceptually straightforward. 如果您了解插入排序和快速排序算法,那么从概念上讲这很简单。 (And if you don't understand them, there are lots of places to read about them, starting with your data structures & algorithms textbook.) (而且,如果您不了解它们,那么可以从数据结构和算法教科书开始到很多地方阅读它们。)

Some algorithms are asymptotically better than others. 一些算法在渐近性上优于其他算法。 In your case, the asymptotic run time of Quicksort is O(N(logN)) while the asymptotic run time of insertion sort is O(N^2). 在您的情况下,Quicksort的渐近运行时间为O(N(logN)),而插入排序的渐近运行时间为O(N ^ 2)。

This means that for large values of N, Quicksort will run faster than insertion sort. 这意味着对于较大的N值,Quicksort的运行速度将比插入排序快。

However, for small values of N, insertion sort may run faster. 但是,对于较小的N值,插入排序可能会运行得更快。 Hence, you can optimize the actual run-time of Quicksort by combining it with insertion sort for small array sizes. 因此,您可以将Quicksort的实际运行时间与较小数组大小的插入排序结合使用,以优化其实际运行时间。

Quicksort is a recursive algorithm, that breaks the original array into smaller arrays and runs recursively on each sub-array. Quicksort是一种递归算法,它将原始数组分解为较小的数组,并在每个子数组上递归运行。 Cut off with insertion sort means that once you reach array sizes smaller than some constant cut-off size, you sort these small arrays using insertion sort instead of continuing the recursion. 使用插入排序进行切除意味着,一旦达到的数组大小小于某个恒定的切除大小,便可以使用插入排序对这些小的数组进行排序,而不是继续递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM