Question

In: Computer Science

public static int example3 (int [] arr ) { int n = arr.length , total =...

public static int example3

(int [] arr ) {

int n = arr.length , total = 0;

for (int j = 0; j < n ; j += 2)

for (int k = 0; k <= j ; k ++)

total += arr [ j ]; return total ;

}

what is the running time of this method?

Solutions

Expert Solution

The time taken by a function in Java can be calculated with the help of java.lang.System.currentTimeMillis() method. which returns the current time in millisecond. By calling this method at the beginning and at the end of function and by taking the difference we measure the time taken by the function. The given program calculates the running time of example3() method by taking an array of size 'n' and each element=1000.

import java.io.*; 

public class Time { 
    public static void main(String[] args) throws IOException
    { 
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n, i, r;
        System.out.println("Enter the size of array");
        n=Integer.parseInt(br.readLine());
        int a[]=new int[n];
        for(i=0; i<n; i++)
        {
            a[i]=1000;
        } 
        long s = System.currentTimeMillis(); // Starting of the function
        r=example3(a); 
        long e = System.currentTimeMillis(); // ending of the function
        System.out.println("The running time of method example3() is " + 
            (e - s) + " ms"); 
    } 

    public static int example3(int [] arr ) // function whose running time is to be calculated
    {

        int n = arr.length , total = 0;

        for (int j = 0; j < n ; j += 2)

            for (int k = 0; k <= j ; k ++)

                total += arr [ j ];
        return total ;

    }
} 

Output:

Enter the size of array
10000
The running time of method example3() is 22 ms


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT