弄清楚时间的复杂性

时间:2018-05-13 16:00:05

标签: algorithm time-complexity complexity-theory

我是一名学生并且学习时间复杂性。我陷入了下面的问题。我已经搜索过它,但找不到任何相关内容。任何人都可以弄清楚下面算法的时间复杂度,或者只是让我知道在哪里找到答案。

1:  procedure MysteryAlg(x , n)
2:      if n = 0 then
3:          return 1
4:      end if
5:      if n = 1 then
6:          return x
7:      end if
8:      if n is even then
9:          return MysteryAlg(x * x, n/2)
10:     else
11:         return MysteryAlg(x * x, n/2) * x
12:     end if
13:  end procedure

谢谢。

1 个答案:

答案 0 :(得分:1)

您编写的算法命名为"通过平方"指数排序。您可以从此处获取更多信息:https://en.wikipedia.org/wiki/Exponentiation_by_squaring

查看标题为" 基本方法"

的部分

需要O(log2(n))操作。因此,您可以将其视为算法的复杂性。

相关问题