Class VectorSum
java.lang.Object
topics.divideconquer.VectorSum
DIVIDE AND CONQUER PROBLEM: CALCULATE THE SUM OF n ELEMENTS IN A VECTOR
- Author:
- vicegd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintsum1(int[] v) This method iteratively calculates the sum with a linear complexity O(n)intsum2(int[] v) This method recursively calculates the sum with a linear complexity O(n).intsum3(int[] v) This method recursively calculates the sum with a linear complexity O(n).
-
Constructor Details
-
VectorSum
public VectorSum()
-
-
Method Details
-
sum1
public int sum1(int[] v) This method iteratively calculates the sum with a linear complexity O(n)- Parameters:
v- Array to be used for the addition- Returns:
- Addition of all the elements of the array
-
sum2
public int sum2(int[] v) This method recursively calculates the sum with a linear complexity O(n). DandC by subtraction with a=1,b=1,k=0 - O(n)- Parameters:
v- Array to be used for the addition- Returns:
- Addition of all the elements of the array
-
sum3
public int sum3(int[] v) This method recursively calculates the sum with a linear complexity O(n). DandC by division with a=2,b=2,k=0 - O(n)- Parameters:
v- Array to be used for the addition- Returns:
- Addition of all the elements of the array
-