Lab 1: Running Time Analysis

Assignment is attached.

Example:

public class Lab1 {
public static void main(String[] args) {
int[] a = { 4, -3, 5, -2, -1, 2, 6, -2, 4, -3, 5, -2, -1, 2, 6, -2 };
int answer;
answer = algorithm1(a);
System.out.println(“The answer is ” + answer);
answer = algorithm2(a);
System.out.println(“The answer is ” + answer);
}
public static int algorithm1(int[] a) {
int maxSum = 0;
for (int i = 0; i < a.length; ++i) for (int j = i; j < a.length; ++j) { int thisSum = 0; for (int k = i; k <= j; ++k) thisSum += a[k]; if (thisSum > maxSum)
maxSum = thisSum;
}
return maxSum;
}
public static int algorithm2(int[] a) {
int maxSum = 0, thisSum = 0;
for (int j = 0; j < a.length; ++j) { thisSum += a[j]; if (thisSum > maxSum)
maxSum = thisSum;
else if (thisSum < 0) thisSum = 0; } return maxSum; } }

Last Completed Projects

topic title academic level Writer delivered