开发者

Sorted array Big o notation

I just have a simple quest开发者_如何转开发ion, why is the big O notation of a sorted array O(log N)? It will be a sorted array.


Big O notation generally makes sense in context of an algorithm. What operation are you considering when you say the Big O notation is O(log n).

If you mean searching, then it is O(log n) because you can use binary search. Which essentially means you look at the middle element of you array, and if it is greater than the element you're searching for, you then search the larger half (in the same way), and vice versa (assuming you haven't yet found your element of course). You can read a more detailed description on wikipedia.

At each step of searching (looking the middle element), you are cutting the size of the array you must search in half since you can now know which side of the middle element your search element must lie. Of course this only works with sorted arrays. For non-sorted arrays, the only search algorithm you can use is linear search where you examine every element in the array which will take on average n/2 inspections.

In general, Big O describes the runtime characteristics of algorithms, so you can't just ask, what is the Big O of a sorted array, it must be some operation on the array. However, you can consider Big O in terms of the space (memory) taken by some data structure. In this case a sorted array still takes O(n) space to store N elements.


The question is somewhat bogus. The complexity doesn't apply to the array but actually to the algorithm that sorts the array (I assume you mean runtime complexity not memory).

So depending on the used algorithm the X in O(X) for sorting an array can vary strongly.

Check pages these for a starter on complexity in general and in special case array sorting.

Introduction to algorithm complexity

Sorting Arrays: Algorithms and Efficiency

Wikipedia: Computational complexity theory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜