【问题标题】:Help verifying Big O帮助验证 Big O
【发布时间】:2011-05-31 03:40:52
【问题描述】:

嘿,所以我正在尝试验证一些排序算法。

  • 插入排序

  • 合并排序

  • 使用“三的中位数”分区和 10 的截断进行快速排序(对小数组部分使用插入排序)

分析:
讨论了插入排序的最坏情况运行时间为 O(n2),排序输入的实际运行时间为 O(n)(只要内部循环编码正确)。合并排序被讨论为所有输入的 O(n log n)。使用“三的中值”分区和合理的截止值的快速排序被讨论为所有输入的 O(n Log(n)),但在实践中比 Mergesort 更快。您的时间安排是否验证了这些计算?

注意 N = Max/2 和 2N = MAX

运行程序后发现

The diff for insertion sorted with sorted max/2 is 0.000307083129882812
The diff for insertion sorted with sorted max is 0.000623941421508789
The diff for insertion reverse with sorted max/2 is 0.000306129455566406
The diff for insertion reverse with sorted max is 0.000745058059692383
The diff for insertion random with sorted max/2 is 2.39158606529236
The diff for insertion random with sorted max is 9.72073698043823
The diff for merge sort with sorted max/2 is 0.00736188888549805
The diff for merge sort with sorted max is 0.0154471397399902
The diff for merge reverse with sorted max/2 is 0.00730609893798828
The diff for merge reverse with sorted max is 0.0154309272766113
The diff for merge random with sorted max/2 is 0.0109999179840088
The diff for merge random with sorted max is 0.0232758522033691
The diff for quick sorted with sorted max/2 is 3.10367894172668
The diff for quick sorted with sorted max is 12.5512340068817
The diff for quick reverse with sorted max/2 is 3.09689497947693
The diff for quick reverse with sorted max is 12.5547797679901
The diff for quick random with sorted max/2 is 0.0112619400024414
The diff for quick random with sorted max is 0.0221798419952393

我知道插入排序工作正常,因为对于随机,我做了 9.72073698043823/ 2.39158606529236 ~= 4 = 22 = O(n2)

但我不知道如何验证其他的是否为 O(n Log(n))。请帮忙

【问题讨论】:

  • 您需要获取更多点的数据才能得出任何结论。

标签: sorting big-o


【解决方案1】:

让我提醒你,f(n)=O(n) 表示当 n 变得非常大时 f(n)/n => 常数时的极限

没说的:

  • 这个常数可以很大
  • 对于小的值,它意味着一些东西:例如10^9/n+n 是 O(n) 但对于 n=1,it0s 10^9+1 ;-)
  • O(something) 不是参数杀手,您的数据拓扑有时可能会影响算法(例如:在“几乎”排序的数据上,冒泡排序表现良好)

如果您想得出结论,请使用与您的应用程序相关的大样本进行测试,不要过早得出结论(请注意,如果您使用现代 CPU,它可能会在缓存、流水线和多核方面欺骗您(您可以做什么)排序)

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多