Implemented the ladder method with recursion. The first version will find the largest value. The second version will find the index of the largest value. In principle, you could just implement the second version and use that index to find the largest value for the first version; you are not supposed to do so. For both versions, you may only use a constant amount of extra memory, other than the extra memory that the recursion automatically uses on the stack (or heap) during execution. You can assume that the array is stored in locations 1, . . . , n or locations 0, . . . , n − 1, whichever you prefer. Assume that n is a power of 2.
(a) Write the pseudo-code for a recursive implementation of the ladder method of finding the largest of n values.
(b) Write the pseudo-code for a recursive implementation of the ladder method of finding the index of the largest of n values.
public static int findMaxIndex(double[] values){
// verdict the length of the values array
int noOfValues = values.length;
// declare max and maxIndex to store max and
// maxIndex
double max = 0.0;
int maxIndex;
for(int i =0; i < noOfValues ; i++){
//verifying if the current value is greater than max
//if greater afterwards storing the value in max and
// index in maxIndex
if(values[i] > max){
max = values[i];
maxIndex = i;
}
}
// recurring maxIndex
return maxIndex;
}
Implemented the ladder method with recursion. The first version will find the largest value. The second...
Unrolling Recursion
The objective of this problem is to simulate recursion using stacks
and loops. A synthetic linear recursive procedure for this problem
is provided in Code Fragment 1. A recursive function such as the
one described is intuitive and easily understandable. On calling
myRecursion(input), the execution first checks for the stopping
condition. If the stopping condition is not met, then operations
inside the recursive call are performed. This can include
operations on local variables. The operations inside the function...
USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...
a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>> implements MinHeapInterface<T> { private T[] heap; // Array of heap entries; ignore heap[0] private int...
Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...
Write a method called printReverse() that
takes a string and uses recursion to print the contents of the
string in reverse order. The string itself should
not be reversed; it must be left in its
original form.
The method has the following header:
void printReverse(String s, int i)
where s is a reference to the string, and i is an integer
parameter that you may use as you see fit. You do not need
to code up this method as...
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...