You are here:

Java/Big O Notation problem

Advertisement


Question
I wanted to evaluate an algorithm and my adviser told me to use the Big O notation. I really don't know how to use and how to determine using the Big O notation if an algorithm is a good one or not.
How will I evaluate the algorithm? Is it by statement?class? method?

Please help me on these. Thanks a lot.

Answer
Your processor can probably help you better than I can.

The Big O of an algorithm is a calculation of how many steps an algorithm takes in relation to its input size...

Big O of 1: constant time. The algorithm always runs within a certain time regardless of the input size. E.g. An algorithm that determines if the input word starts with a capitol letter.

Big O of n: linear time. If the input size is twice as big, the algorithm will takes about twice as long. E.g. find the biggest number in a list of numbers. A algorithm with a single for loop.

Big O of n squared: If the algorithm takes a second with input size of 10, then it will take 4 seconds with input size of 20, 9 seconds with input size of 30, ect... If you see an algorithm with 2 imbedded for loops, its probably n squared.

Big O of log n: If the input size is twice as big, the algorithm will only take a linear amount of time longer. E.g. a search algorithm to find a number in a sorted list. Start in the middle, and then you know to search higher or lower.

Big O of n log n: A combination of linear time and log n. An algorithm that does a search on a sorted list for every element in the list (for example, trying to find all numbers in a sorted list that have a negative counterpart).

Big O of n factorial (n!): A really slow algorithm.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2012 About.com, a part of The New York Times Company. All rights reserved.