4 Replies - 2693 Views - Last Post: 28 November 2013 - 03:02 PM

#1 hashom   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 75
  • Joined: 27-November 13

Question in algorithm

Posted 27 November 2013 - 12:09 PM

hello guys

i have exam after 1 week and i need help in this question

Assume that we have two algorithms for solving one specific algorithm. The first algorithm (A) takes time n, while algorithm (B) takes time 3lg n+5. When should we choose algorithm A, and when should we choose algorithm B? :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Question in algorithm

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: Question in algorithm

Posted 27 November 2013 - 12:23 PM

If you graph the two algorithms against each other you can see exactly where one algorithm is better (closer to 0 on the y) than the other:
http://www.wolframal...8427ea9nbpa02ht
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Question in algorithm

Posted 27 November 2013 - 02:00 PM

If algorithm A takes time n, then it is O(n). Similarly, algorithm B is O(log(n)). Which is the dominant complexity class?
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Question in algorithm

Posted 27 November 2013 - 02:37 PM

This is where I jump in again where the complexity only tells you a little bit of the story. It tells you how the algorithms will behave as the inputs grow, but it is the actual graphs that will give you the real data.

Let's say that you are comparing an O(n) algorithm vs. an O(n2). On the surface it looks like the O(n) algorithm looks much better. But what if each step of the O(n) algorithm takes 1 year to complete, while each step of the O(n2) requires a microsecond, will you still pick the O(n) algorithm?
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Question in algorithm

Posted 28 November 2013 - 03:02 PM

That's a good point. A realistic example of this would be to use Mergesort on arrays, but switch to insertion sort for the smaller cases (arrays with <= 10 elements), since Insertion Sort does better for smaller data sets.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1