×

How to calculate time complexity of any program in Java

Java :

Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is not in C or object oriented programming language is platform independence.

Not only the platform independence there are many other features in java such as secure, highly interpreted, robust, portable etc. We have a JVM ( Java Virtual Machine ) , JRE ( Java Runtime Environment ) , JDK ( Java Development Kit ). It is used to convert .class file to .exe file.

Time complexity :

The length of the input determines how long it takes an algorithm to run, which is known as temporal complexity. It calculates how long it takes for each algorithm's code statement to run. It won't look at an algorithm's overall execution time.

Instead, it will provide details regarding the variation it may be increase or decrease in execution time when an algorithm's number of operations whether it may be increase or decrease. Yes, as the definition states, the only factor affecting the amount of time required is the length of the input.

The efficiency of an algorithm can be determined by its time complexity. Even if we are aware that there are other ways to approach an issue in programming, understanding how an algorithm operates effectively might improve our job.

Understanding how to analyze programs and algorithms utilizing Time complexity might help us become more productive programmers by getting them to perform in the desired ideal conditions.

The definition of the algorithm is as shown below :

1. Given two input matrices, one of which is an n-by-n square matrix

2. Using the function, the values of each member in both matrices are chosen at random.

3. Initialized with an output matrix that contains 0 values and has an order matching the order of the input matrix.

4. The product of multiplying each element of X by each element of Y is recorded in the result matrix.

5. After that, the matrix is changed to a list type.

6. The total value for each item in the result list is summed to provide the answer.

Let's say that the cost function C is measured in terms of the amount of time it takes a function to run, and that n is the number of times the statement will be executed by an algorithm.

For instance, if the algorithm is set up to execute the print function 1000 times and it takes 1 microsecond (C) to do so (n),

Thus the total run time is equal to (C * n) = 1 microsecond multiplied by 1000, or 1 millisecond.

    There are different kind of notations to mention the time complexity such as

  • O ( 1 ) = > constant
  • O ( n ) = > Linear
  • O ( n power 2 ) = > Quadratic
  • O ( log n ) = > Logarithmic
  • O ( n power p ) = > Polynomial
  • O ( k power n ) = > Exponential

Now let us see some examples and let us calculate the time complexity of that programs:

Example 1:

int n = 5;
System.out.println ( n );
System.out.println ( n );

Here the time complexity of the above code is O ( 1 ) because how many times we print then value the time complexity remains constant.

Example 2:

for ( int i=0 ; i<n ; i++)
{
System.out.println ( i );
}

Here the time complexity of the above code is O ( n ) since the loop repeats ‘ n ‘ times.

Example 3:

for ( int i=0 ; i<n ; i=i*2)
{
System.out.println ( i );
}

Here the time complexity of the above code is O ( log n ) .

Example 4 :

for ( int i=0 ; i<n ; i++)
{
for ( int j=0 ; j<n ; j=j*2 )
{
System.out.println ( i );
System.out.println ( j );
} // j loop
} // I loop

Here the time complexity of the above code is O ( n log n ) since the outer loop repeats ‘ n ‘ times and the inner loop has logarithmic function.

As shown above we can calculate the time complexity of any of the program. We can also use the step count method to calculate the time complexity.


Related Topics

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java String Reader

StreamReaderClass: This class is present in the java.io package. It is a character stream in which string act as a source.This method provides to read characters from a string. Character Stream: This class...

3 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.