LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Finding a real value for constant in Big O notation (https://www.linuxquestions.org/questions/programming-9/finding-a-real-value-for-constant-in-big-o-notation-104464/)

oulevon 10-15-2003 08:15 PM

Finding a real value for constant in Big O notation
 
I need to find a real value for the constant in Big O notation. For example I know that Insertion Sort takes 0(n^2) in its worst case where there are n(n-1)/2 comparisons. I have tested its worst case (when the array is sorted in descending order) for values of n at 1000, 10000, and 100000. Here are my results:

n= 1000
Time = 15 milliseconds

n = 10000
Time = 913 milliseconds

n = 100000
Time = 146768 milliseconds

My question is how do I apply this data in getting the value of c in the equation
f(n) = c(g(n))

?

Do I graph my results and then keep increasing the constant c until it becomes a tight upper bound for the sort? Any ideas would help, thanks.

Claus 10-16-2003 02:32 PM

Look, i think you should use a statistical regression...

Y = a + bt + ct^2

and find the parameters a, b, c.

then if you want constant such that F > y for every t>k,

Y = a + bt + ct^2 < at^2 + bt^2 + ct^2 = (a + b + c)t^2

then, you can say that C = a + b + c is your constant.

I hope you understand me.

jharris 10-16-2003 02:37 PM

Damn guys! I thought I escaped this when I graduated - you're bringing my repressed memories back :cry: :eek: :scratch:

cheers

Jamie...

Claus 10-16-2003 06:11 PM

Dont feel bad man!!! it's just i like a lot maths....

but anyway, I AM NOT SURE if that's what he really wants :S

Claus 10-16-2003 06:40 PM

Anyway, now that i think, you dont need to do experimental tests to find the constant.

That constant should be any finite number, doesnt matter how big it is. you just need that your worst case function is always greater than n(n-1)/2.

so, your f(t) = n(n-1)/2 = 1/2(n'2 - n)

and you know that

1/2(n'2 - n) < (n'2)/2

So your constant should be just 1/2.

Now, you can do the testing implementing the sort in some programming language, but listen this: the constant will change with every implementation, machine, etc.

The constant, is just a theoric argument to find the order. but doesn't mean that you always can "trap" the function with that constant.

Sorry if my speach sounds confusing, my english is not very advanced.

See a book of algotithms, it can explain you all that stuffs very well...

oulevon 10-17-2003 04:41 PM

Thanks for the help. I needed to do the experiments to find a real constant for the growth rate of the function using empirical results. Since the growth rate is O(n^2) all i needed to do was divide elapsed time by n^2 in order to get c at that given n. I made it more difficult than it really was. Thanks for the replies.

Claus 10-17-2003 05:04 PM

Intelligent way man :)


All times are GMT -5. The time now is 02:39 AM.