Finding a real value for constant in Big O notation
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.