In three of the exercises below there is given a code of a method named find, and a fourth one
named printMany. Analyze the codes through the following points.
Explain your choice of the input size n and in terms of O(n) scale determine the running time
(number of steps) T(n) of the algorithms represented by the methods.
Use the simplest and possibly the smallest valid Big-Oh expression.
T(n) can also be considered as the number elementary operations the algorithm must make.
If it applies, point out your estimates for the worst and best cases, and also for the average case
performance if available.
Document each method describing what you consider the method’s precondition and post-
condition.
It is not necessary to run these methods in actual programs, but if the task it performs is dubious,
testing the method with various input in actual applications of the code may help to find its
purpose and the big-Oh estimate.
1) int find( int[] list, int element ){
int answer = 0;
for(int k = 0; k < list.length; k++ )
if (element==list[k])
answer++;
return answer;
}//end method
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
2) staticintfind(int[] arr){
zeroCounter = 0;
(intk = 0; k< arr.length; k++){
(arr[k]==0)
zeroCounter++;
}
(zeroCounter==arr.length)
0;
(zeroCounter < arr.length - 2){
//see maxIndex() definition below
max = maxIndex(arr);
arr[max] = 0;
//see display() definition below
display(arr);
zeroCounter ++;
}
maxIndex(arr);
//end method
//helper methods
3) staticint maxIndex(int[]arr){
int maxindex = 0;
for(int k = 0 ; k< arr.length; k++){
// note the use of absolute value
if(Math.abs(arr[maxindex]) < Math.abs(arr[k]))
maxindex = k;
}
return maxindex;
}
staticvoid display(int[]arr){
System.out.println();
for(int k = 0 ; k< arr.length; k++)
System.out.print(arr[k]+” “);
System.out.println();
}
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
3) int find(int[] num){
int answer = 0;
for(int k = 0; k < num.length; k++ )
for(int j = k; j< num.length; j++){
int current = 0;
for(int i = k; i<=j; i++)
current += num[i];
if (current > answer)
answer = current;
}
return answer;
}
Note: Given two indices i<=j of an array of integers num, the sum
num[i]+ num[i+1] + …+ num[j] is called a sub-sum
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
4) void printMany(int[]arr){
int N = arr.length;
for(int k = 0 ; k< N; k++){
int p = k;
while(p>0){
System.out.println(arr[p]+\" \");
p = p/2;
}
}
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
Solution
1) int find( int[] list, int element ){
int answer = 0;
for(int k = 0; k < list.length; k++ ) //traversing array start to end
if (element==list[k]) //checking for element
answer++; // if element found, increase by 1
return answer;
}//end method
In the function given above, we can.
In three of the exercises below there is given a code of a method na.pdf
1. In three of the exercises below there is given a code of a method named find, and a fourth one
named printMany. Analyze the codes through the following points.
Explain your choice of the input size n and in terms of O(n) scale determine the running time
(number of steps) T(n) of the algorithms represented by the methods.
Use the simplest and possibly the smallest valid Big-Oh expression.
T(n) can also be considered as the number elementary operations the algorithm must make.
If it applies, point out your estimates for the worst and best cases, and also for the average case
performance if available.
Document each method describing what you consider the method’s precondition and post-
condition.
It is not necessary to run these methods in actual programs, but if the task it performs is dubious,
testing the method with various input in actual applications of the code may help to find its
purpose and the big-Oh estimate.
1) int find( int[] list, int element ){
int answer = 0;
for(int k = 0; k < list.length; k++ )
if (element==list[k])
answer++;
return answer;
}//end method
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
2) staticintfind(int[] arr){
zeroCounter = 0;
(intk = 0; k< arr.length; k++){
(arr[k]==0)
zeroCounter++;
}
(zeroCounter==arr.length)
0;
(zeroCounter < arr.length - 2){
//see maxIndex() definition below
2. max = maxIndex(arr);
arr[max] = 0;
//see display() definition below
display(arr);
zeroCounter ++;
}
maxIndex(arr);
//end method
//helper methods
3) staticint maxIndex(int[]arr){
int maxindex = 0;
for(int k = 0 ; k< arr.length; k++){
// note the use of absolute value
if(Math.abs(arr[maxindex]) < Math.abs(arr[k]))
maxindex = k;
}
return maxindex;
}
staticvoid display(int[]arr){
System.out.println();
for(int k = 0 ; k< arr.length; k++)
System.out.print(arr[k]+” “);
System.out.println();
}
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
3) int find(int[] num){
int answer = 0;
for(int k = 0; k < num.length; k++ )
for(int j = k; j< num.length; j++){
int current = 0;
for(int i = k; i<=j; i++)
current += num[i];
3. if (current > answer)
answer = current;
}
return answer;
}
Note: Given two indices i<=j of an array of integers num, the sum
num[i]+ num[i+1] + …+ num[j] is called a sub-sum
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
4) void printMany(int[]arr){
int N = arr.length;
for(int k = 0 ; k< N; k++){
int p = k;
while(p>0){
System.out.println(arr[p]+" ");
p = p/2;
}
}
Comments
What does the method do:
Input size n =
Worst case T(n) = O(________) Best case T(n) = O(________)
Solution
1) int find( int[] list, int element ){
int answer = 0;
for(int k = 0; k < list.length; k++ ) //traversing array start to end
if (element==list[k]) //checking for element
answer++; // if element found, increase by 1
return answer;
}//end method
4. In the function given above, we can see an array ‘list’, and an integer ‘element’ being accepted
as parameters.
We initialize an integer ‘answer’, with an initial value of 0.
What we are doing with this function is searching for the frequency of an element, in the list.
The for loop starts at index 0, and goes on till the end of the array. At each iteration, the if
statement checks whether the ‘element’ input by the user, is present in the list at the ‘kth’
position. Every time the element matches with the element present in the list, the value of
‘answer is incremented by 1.
When the loop terminates, after having traversed the entire array, the variable ‘answer’ has a
value stored, which is equal to the number of times the element input by the user, was found in
the list.
Hence, we get the frequency of the occurrence of an element in a list through this function.
Note: This is a classic example of ‘Linear search’ for an element, where we search the entire list
for an element.
It has a complexity T(n)= O(n) for Both Worst Case, and Best Case.
2) code not clear.
3)
3) int find(int[] num){
int answer = 0;
for(int k = 0; k < num.length; k++ ) //will traverse entire list
for(int j = k; j< num.length; j++){
int current = 0;
for(int i = k; i<=j; i++) // will traverse from k to j every time
current += num[i];
if (current > answer)
answer = current; // computing largest sub sum of list
}
return answer; //returning largest sub sum of list
}
On inspection, we can see that for k=0, and j=0, the i loop runs one time, and for k=0,j=1, the i
loop runs twice, and so on. Therefore, we can say that the inner most loop is bound between the
‘k’ loop and the ‘j’ loop, ‘k’ being the lower bound, and ‘j’ being the upper bound.
The inner most loop, bound between the values of k and j, calculates the sum of the elements
ranging from k to j (the sub-sum). If this sum is found to be greater than the previously computed
sub sum, then the answer is updated.
The ultimate motive of this method is to find the largest sub sum of the given list.
5. Let num.length() be n,
Then the outmost loop runs n times.
The second loop runs a total of n times when k=0, n-1 times when k=1, n-2 times for k=3 and so
on, running for a total of n(n+1)/2 times (sum of numbers of 1-n;
The third loop runs 1 time for k=0,j=0, 2 times for k=0,j=1 and so on.
Therefore the complexity of this method is given by O(n^3).
T(n)=O(n^3) (both worst and best cases).
4) void printMany(int[]arr){
int N = arr.length;
for(int k = 0 ; k< N; k++){
int p = k;
while(p>0){
System.out.println(arr[p]+" ");
p = p/2;
}
}
In this function, the outer k loop runs from 0 to N i.e. the entire list.
For k=0, p-0, and hence the inner while loop does not run.
For k=1, p=1, and hence the inner while loop runs 1 time, and prints 1 element.
For k=2, p=2 and hence the inner while loop runs twice, and prints two elements.
For k=3, p=3 and hence the inner while loop runs twice, and prints two elements.
For k=4, p=4 and hence the inner while loop runs thrice, and prints three elements.
For k=5, p=5 and hence the inner while loop runs thrice, and prints three elements.
And so on,
On further inspection, we find that the inner while loop runs and prints twice the number of
elements, it has printed for a certain number of iterations of the outer loop i.e. the inner loop first
runs 0 times, then it runs one time, then it runs two times, then it runs four times, and so on, or in
other words the number of elements printed is growing in multiples of two.
Analysis:
The outer loop runs N times. The inner loop runs 1+2+4… times, i.e. in a geometric progression
with common ratio of 2. The formula for sum of a GP is
S=a({r^n}-1)/(r-1)
Where r is the common ratio, a the first term.
The complexity can then be represented as