×

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

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.