Simple “maximum value in array” and complexity calculations

前端 未结 4 1083
眼角桃花
眼角桃花 2021-02-20 07:15

I\'m pretty new to this stuff and I need your help.

I should build an efficient simple algorithm which returns the maximum value in an array with size of n which contain

4条回答
  •  情歌与酒
    2021-02-20 07:47

    If there is no prior information about the array (e.g. it's sorted), then there is no worst case or best case, and You have to scan all the elements to find out the Max, and it takes O(n) times.

    Also, knowing the probability distribution of getting max value for each cell is useless in general (unless it reduces your search space. e.g., If you know that only constant number of cells have non-zero probability of getting the max value, then you just need to search those cells and it takes constant time). Thus, in general

    Best-case running time = Worst-case running time = average running time = O(n)

提交回复
热议问题