Python-嵌套列表中二进制搜索算法的时间复杂度

时间:2018-12-04 17:36:54

标签: python time-complexity complexity-theory binary-search

我有一个嵌套的数字列表,其中有n个行和列,每个行和列中的每个数字都在增加。

例如:

db.communications.aggregate([
  { "$unwind": "$messages" },
  { "$lookup": {
    "from": "users",
    "localField": "messages.authorId",
    "foreignField": "_id",
    "as": "messages.authorId"
  }},
  { "$unwind": "$messages.authorId" },
  { "$group": {
    "_id": "$_id",
    "messages": { "$push": "$messages" },
    "type": { "$first": "$type" },
    "title": { "$first": "$title" },
    "receivers": { "$first": "$receivers" }
  }}
])

我需要使用二进制搜索来查找元素是否在列表中,并且算法最坏情况下的时间复杂度必须为O(n)。

我有此代码:

A = [[19, 30, 31, 45, 57],
     [25, 32, 32, 51, 69],
     [33, 35, 38, 58, 78],
     [34, 49, 67, 84, 102],
     [44, 54, 73, 90, 115]]

我认为这段代码的时间复杂度为O(n log n),但我对此不确定。那么这段代码的实时复杂度是多少?

0 个答案:

没有答案