1

I recently read about time complexity and I found out that Quick sort has an average time complexity of O(nlog(n)).

Question 1: What I do not understand is how is the log(n) present in the time complexity equation?

Question 2: Why do we always use the big O notation to find the time complexity of algorithms? Why don't we use other notations?

praxmon
  • 5,009
  • 22
  • 74
  • 121
  • 1: it doesn't represent the time, it's merely a classification of the algorithm as a function: http://www.wolframalpha.com/input/?i=log2%28n%29 2: see (1) – Flavius Aug 15 '12 at 04:11
  • @Flavius Can you please explain classification of an algorithm? – praxmon Aug 15 '12 at 04:12
  • Sure: http://en.wikipedia.org/wiki/Algorithm#By_complexity – Flavius Aug 15 '12 at 04:14
  • 1
    This isn't really the place for this type of question. Take a computer science course, or watch one online: http://www.aduni.org/courses/ – Benjamin Lindley Aug 15 '12 at 04:15
  • I'd recommend reading any book about algorithms. 'The Algorithm Design Manual' by Skiena has a very nice explanation of both big-O notation and sorting complexities, for example. – dragonroot Aug 15 '12 at 04:18
  • 1
    *"Why do we always use the big O notation to find the time complexity of algorithms?"*... "we" don't. – user541686 Aug 15 '12 at 04:27
  • The `n` represents the number of elements you are operating on. So a time complexity of `log(n)` indicates that the algorithm's running time increases logarithmically (much slower) in relation to the number of elements. Multiplication by `n` means there is a linear time complexity to each logarithmic operation. The `log(n)` itself is present because of the divide-and-conquer approach that quicksort uses: Each time you split the problem into two (ideally equal) parts, that reduces the size of the problem by half. That is logarithmic behaviour. – paddy Aug 15 '12 at 04:27
  • 2
    Note to @paddy's comment: it's "by 2" and "in half" because when computer scientists say `log`, they really mean `log_2`. – Flavius Aug 15 '12 at 04:29

4 Answers4

11

How did the logn got into the complexity formula?

  • For each step, you invoke the algorithm recursively on the first and second half.
  • Thus - the total number of steps needed, is the number of times it will take to reach from n to 1 if you devide the problem by 2 each step.
    So you are actually looking for a k such that:

    n / 2 /2 / 2 / ... /2 = 1
            ^
         (k times) 
    

    But, note that the equation is actually: n / 2^k = 1. Since 2^logn = n, we get k = logn. So the number of steps (iterations) the algorithm requires is O(logn), which will make the algorithm O(nlogn) - since each iteration is O(n).

Note: The complexity in here is not exact, quicksort in rare cases decays to O(n^2), it is depends on the pivot selection. The "devide the problem by 2 each step" is a simplification, but it does not change the average analyzis of the algorithm.

Why use big O notation?
It is simple and platform independent.
The exact number of op (and sometimes even comparisons) is platform dependent. (If instruction set A is richer then instruction set B, it will probably need more ops).
It is definetly not the only method used. For real life applications, the exact run time (in seconds) is very important factor and is often used.

So, in short - big O notation gives us easy to calculate - platform independent approximation on how will the algorithm behave asymptotically (at infinity), which can divide the "family" of algorithm into subsets of their complexity, and let us compare easily between them.

Also, other notations that are used are small o, theta and big/small omega.

amit
  • 175,853
  • 27
  • 231
  • 333
2

Ques 1. Quick sort's worst case time complexity is O(n^2),whereas average case complexity is O(nlogn). logn factor depends upon the pivot, how the algorithm choose it.

Quick sort worst case time complexity occur when pivot produces two regions one of size 1 element and other of size (n-1) elements recursively.Whereas average case occurs when pivot chooses two regions such that both regions produced have a size of n/2.

So recursive relation produced is T(n)=2T(n/2)+Θ(n).

                  f(n)=Θ(n)     
                  h(n)=n^log2(2)=>n 
                  since f(n) = h(n) so
                  T(n)= f(n)logn =>n(logn).

(here we are using Θ notation since worst case complexity(big O) of quick sort is O(n^2) and here we are calculating average case complexity.)

Ques 2. We use big O notation because it gives the idea of worst case time complexity which limits the algorithm even when the arguments tends infinity,that means the algo will atleast run in this time complexity and cannot go beyond it.

Whereas there are other notations such small o, theta and big/small omega which are used very often as they have limited applications.

adit
  • 39
  • 4
1

Please read Introduction to Algorithms by Cormen et al. In the chapter 3 you will find a good explanation about Complexity Analysis of algorithms. You will find that Big O is not the only asymptotic notation that is used.

André Oriani
  • 3,553
  • 22
  • 29
1

Even though this is more of a question about computer science in general which can be explained more thoroughly by the comments on the question --

Question 1: the log(n) is there to denote that the problem scales at a higher rate than a O(n) problem by a factor log(n). The terms smaller than n*log(n) (like n) are omitted because they scale slower than the largest term.

Question 2: There are other metrics, O (big O) is the worst case rate of problem expansion. See the book links in the comments to see what the others are/mean, as it's better explained there.

Pyrce
  • 8,296
  • 3
  • 31
  • 46
  • It does not answer "how the logn got there" It answers a different question: "what its affect?" – amit Aug 15 '12 at 06:09
  • @amit True, though his question is vague so it's unclear if he was asking why quicksort is O(nlogn) or why T(nlogn) doesn't map to O(n). He didn't seem to grasp the meaning of O notation from what he wrote in his question. I believe other answers address how quicksort reaches O(nlogn), so I'm going to leave me answer as is to cover that possible interpretation of his original first question. – Pyrce Aug 15 '12 at 16:36