Many students want to
understand Time Complexity of algorithms but they don’t. Actually some of them
don’t know what Time Complexity is.
First you have to
understand a thing before writing a code we have to check how much time it’s
algorithm would take to execute but that execution time should be platform
independent (such as: operating system, programming language etc.)
Why would I am saying
platform independent let’s understand with simple example:
We have a function of
code written in java language, c language and php language. All of these
languages will have different lines of code for each language mentioned. That’s
why all codes in different languages will have different execution time. Thus, I
had mentioned platform independent execution time for algorithms.
THIS EXECUTION TIME OF
ALGORITHMS IS SO CALLED TIME COMPLEXITY.
There are various methods
we can find Time complexity but the famous one is Big O notation like:
O(1), O(n), O(n2),
O(log n) etc.
Let’s take an example to
understand what these notations are:
If you have a list of 100
members in a company and you want to get your stolen wallet from them. Here are
the ways to find the wallet.
Big
O(n) Time Complexity
This notation tells that
you go individually to each employee and ask for your wallet.
Big
O(n2) Time Complexity
This notation tells that
you go to each person and ask him to go and ask everyone for his wallet.
Big
O(log n) Time Complexity
This notation tells that
you divide the members equally in a left and right groups and ask is your
wallet is on left side or right side and then you take that group divide it in
two groups and ask again. Repeat the process till you find the member having
your wallet.
Example
using Java language Code:
1.
This code having O(1) Time Complexity.
public static void
main(String arg[]) {
System.out.println(“hello world”);
}
2.
This Code having O(n2) Time Complexity.
public static void
main(String arg[]) {
for (int i = 1; i <= n; i++) {
System.out.println(“hello
world”);
}
}
If you come so far that means you were engaged in
reading my article.
Please Share with your friends to make them understand
and feel us confident to write more for you.
Thank you.
0 comments
If you have any questions or suggestions, you are free to ask, i will appreciate that and, i will try my best...