findin median

What will be the upper bound to choose middle element, if array of size 'n' is already sorted.
[103 byte] By [gomaa] at [2007-10-1 21:51:38]
# 1
What do you think?
rkippena at 2007-7-13 7:54:53 > top of Java-index,Other Topics,Algorithms...
# 2
i think it should be O(1) what about it?
gomaa at 2007-7-13 7:54:53 > top of Java-index,Other Topics,Algorithms...
# 3

Depends if you know how many elements are in the array.

If you know the value of n, then you are correct. If you have to count then it will be O(n). Since most containers generally store a size counter of the number of elements inside, then your answer should be sufficient to please the person who will be marking your assignment.

rkippena at 2007-7-13 7:54:53 > top of Java-index,Other Topics,Algorithms...
# 4
Actually, if the array size was unknown, you could find the first null index in O(logn) time.
rkippena at 2007-7-13 7:54:53 > top of Java-index,Other Topics,Algorithms...