Lab Activity 1.2.A

Source Code

public class ExponentialTiming {

    // Method to calculate time for given input size
    public static double calculateTime(int n, double operationTime) {
        // For O(2^n) complexity, the number of operations is 2^n
        double numberOfOperations = Math.pow(2, n);
        
        // Total time is number of operations times the time per operation
        return numberOfOperations * operationTime;
    }

    public static void main(String[] args) {
        // Define the input sizes and operation time
        int[] inputSizes = {2, 10, 30, 50};
        double operationTime = 0.5; // 0.5 ms per operation
        
        // Display the header for the timing table
        System.out.println("Input Size | Time (ms)");
        System.out.println("----------------------");
        
        // Calculate and display the time for each input size
        for (int n : inputSizes) {
            double time = calculateTime(n, operationTime);
            System.out.printf("%10d | %9.2f\n", n, time);
        }
    }
}

Last updated