Amortized complexity in layman's terms?

后端 未结 6 1447
南笙
南笙 2020-12-02 06:56

Can someone explain amortized complexity in layman\'s terms? I\'ve been having a hard time finding a precise definition online and I don\'t know how it entirely relates to

6条回答
  •  感情败类
    2020-12-02 07:29

    Say you are trying to find the kth smallest element of an unsorted array. Sorting the array would be O(n logn). So then finding the kth smallest number is just locating the index, so O(1).

    Since the array is already sorted, we never have to sort again. We will never hit the worst case scenario more than once.

    If we perform n queries of trying to locate kth smallest, it will still be O(n logn) because it dominates over O(1). If we average the time of each operation it will be:

    (n logn)/n or O(logn). So, Time Complexity/ Number of Operations.

    This is amortized complexity.

    I think this is how it goes, im just learning it too..

提交回复
热议问题