Can someone help me form the math notation and define the ti

Can someone help me form the math notation and define the time complexity from the following code:

import java.util.Arrays;
import java.util.Random;

public class ClassExample1 {


public static void main(String[] args) {
  
benchmark (8, 10);
}

  
public static void benchmark(int x, int y)
{
  
// Then you call the method
int [][] myTests = getArray(x,y);
for (int []vector : myTests )
{
myMethod(vector);
}
  
  
  
  
}

public static int[][] getArray(int x, int y)
{
int [][] result = new int[x][];
  
for (int i = 0; i < x; i += 1)
{
int [] inputinput;
inputinput = new int [(int)Math.pow(y, i)];
result[i] = populate(inputinput);
  
}
return result;
}

public static int[] populate (int[] input)
{
Random rand = new Random();
for (int i = 0; i < input.length; i += 1)
{
input[i] = rand.nextInt(10000) - 5000;
}
return input;
}

public static void myMethod (int [] input)
{

// Then you time when the method finishes
// First you count the start time
long start = System.nanoTime();
Arrays.sort(input);
long finish = System.nanoTime();
// Then you solve for the actual execution time
long algTime = finish - start;
System.out.println(\"It takes \"+ algTime +
\" nano seconds to sort an array of size \" + input.length);
}
}

Solution

The program takes only bechmark i.e how many number of 1D array are needed and a value to power of array number for each array size.Using random function, all the 1D array values are initialized from 5000 to 10000 range and performing sort on each 1D array using java util sort method.

Java Code:

=======

import java.util.Arrays;

import java.util.Random;

public class ClassExample1 {
//Here calling benchmark function with number of ID arrays and number of elements in each array
public static void main(String[] args) {
  
benchmark (8, 10);
}
  
public static void benchmark(int x, int y)
{
  
// Get array elements by calling getArray with parameters x and y
int [][] myTests = getArray(x,y);
for (int []vector : myTests )
{
myMethod(vector);
}
  
  
  
  
}
//Initialzation size of each x 1D array with pow function in math and populating the values randomly.
public static int[][] getArray(int x, int y)
{
int [][] result = new int[x][];
  
for (int i = 0; i < x; i += 1)
{
int [] inputinput;
inputinput = new int [(int)Math.pow(y, i)];
result[i] = populate(inputinput);
  
}
return result;
}
//Initializes random values for each 1D Array
public static int[] populate (int[] input)
{
Random rand = new Random();
for (int i = 0; i < input.length; i += 1)
{
input[i] = rand.nextInt(10000) - 5000;//range random number from 5000 to 10000
}
return input;
}
//Calculates time for storing each 1D Array
public static void myMethod (int [] input)
{

// Then you time when the method finishes
// First you count the start time
long start = System.nanoTime();//starting time
Arrays.sort(input);
long finish = System.nanoTime();
// Then you solve for the actual execution time
long algTime = finish - start;
System.out.println(\"It takes \"+ algTime +
\" nano seconds to sort an array of size \" + input.length);
}
}

Output:

=====

It takes 12531239 nano seconds to sort an array of size 1
It takes 2851 nano seconds to sort an array of size 10
It takes 45044 nano seconds to sort an array of size 100
It takes 592408 nano seconds to sort an array of size 1000
It takes 6472592 nano seconds to sort an array of size 10000
It takes 13942414 nano seconds to sort an array of size 100000
It takes 129498015 nano seconds to sort an array of size 1000000
It takes 690936605 nano seconds to sort an array of size 10000000

Note: Time complexity is calculated using system nanoTime method.

Can someone help me form the math notation and define the time complexity from the following code: import java.util.Arrays; import java.util.Random; public clas
Can someone help me form the math notation and define the time complexity from the following code: import java.util.Arrays; import java.util.Random; public clas
Can someone help me form the math notation and define the time complexity from the following code: import java.util.Arrays; import java.util.Random; public clas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site