What does “in constant time” imply?

后端 未结 8 1333
梦谈多话
梦谈多话 2021-01-30 17:21

I work as a programmer, but have no computer science background, so recently I\'ve been following along with the excellent MIT OpenCourseWare intro to Computer Science and Progr

8条回答
  •  情话喂你
    2021-01-30 17:50

    In "constant time" generally means that the time it will take to compute the result is independent of the size of the input.

    For example. Calculating the length of a list / vector in most managed languages is done in constant time no matter how large the list is. The size is stored as a separate field and is updated as the list grows and shrinks. But calculating the count is as simple as reading the field.

    Calculating the size of a doubly linked list is very often not constant time. The list can often be mutated on both ends and hence there is no central place to store a count. Hence determining the length of the list necessitates visiting it and determining how many elements are in there. So as the list grows so does the time it takes to calculate the answer.

提交回复
热议问题