编程语言
首页 > 编程语言> > java – 根据步数确定时间复杂度

java – 根据步数确定时间复杂度

作者:互联网

类文件中有四种不同的算法,它们具有一定的时间复杂度.下面的输出是给定随机数据的数组大小n的每种排序所采取的步骤数.我可以获得一些关于如何确定时间复杂性的帮助吗?如果其中一种具有n ^ 2的时间复杂度,我很确定我必须采用步数并除以n ^ 2并查看它接近的数字,但我不知道该怎么做之后.希望我的问题不是太宽泛.

谢谢!

The array size is 100
Number of steps for Sort 1:         2543 
Number of steps for Sort 2:          813 
Number of steps for Sort 3:       495100 
Number of steps for Sort 4:          776 
The array size is 200
Number of steps for Sort 1:        10381 
Number of steps for Sort 2:         1870 
Number of steps for Sort 3:      3980200 
Number of steps for Sort 4:         1764 
The array size is 300
Number of steps for Sort 1:        20755 
Number of steps for Sort 2:         2999 
Number of steps for Sort 3:     13455300 
Number of steps for Sort 4:         2826 
The array size is 400
Number of steps for Sort 1:        40298 
Number of steps for Sort 2:         4244 
Number of steps for Sort 3:     31920400 
Number of steps for Sort 4:         3933 
The array size is 500
Number of steps for Sort 1:        65165 
Number of steps for Sort 2:         5448 
Number of steps for Sort 3:     62375500 
Number of steps for Sort 4:         5028 
The array size is 600
Number of steps for Sort 1:        90241 
Number of steps for Sort 2:         6697 
Number of steps for Sort 3:    107820600 
Number of steps for Sort 4:         6178 
The array size is 700
Number of steps for Sort 1:       122291 
Number of steps for Sort 2:         8030 
Number of steps for Sort 3:    171255700 
Number of steps for Sort 4:         7416 
The array size is 800
Number of steps for Sort 1:       157374 
Number of steps for Sort 2:         9053 
Number of steps for Sort 3:    255680800 
Number of steps for Sort 4:         8627 
The array size is 900
Number of steps for Sort 1:       202401 
Number of steps for Sort 2:        10674 
Number of steps for Sort 3:    364095900 
Number of steps for Sort 4:         9842 
The array size is 1000
Number of steps for Sort 1:       243032 
Number of steps for Sort 2:        12047 
Number of steps for Sort 3:    499501000 
Number of steps for Sort 4:        11101 
> 

解决方法:

标准技巧是在对数日志图上绘制值.

如果你有一个关系y = A n ^ k然后log(y)= log(A)k log(n)你可以看到顺序就是这条线的斜率.

例如,这是第一组数据的log(n)log(v)图:

enter image description here

这表明k = 2的值 – 所以你的数据可能是O(n ^ 2).

使用八度音阶生成图像

> n= [ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 ]
> v = [2543, 10381, 20755, 40298, 65165, 90241, 122291, 157374, 202401, 243032]
> plot(log(n), log(v))

可以使用适当的线性拟合进行更好的估计.

使用可以获得订单的更高准确度估计

> polyfit(log(n), log(v), 1)
ans =
    1.9892  -1.3227

其中显示曲线近似为v = 0.27n ^ 2,因为exp(-1.3)大约为0.27.

值得注意的是,这只会在O(n ^ p log ^ q n)类型行为中获得p项,并且在这些情况下曲线将在顶部弯曲.

标签:big-theta,java,algorithm,big-o,time-complexity
来源: https://codeday.me/bug/20190829/1762806.html