Concept explainers
Explanation of Solution
Recursive method to arrange the elements is less than or equal to “n”:
Create the method arrangingElements() that accepts the input parameter as “a”, “n”, “low”, and “high” to arrange the elements is less than or equal to “n” before any integer greater than “n” by calling this method recursively.
Method definition:
//Define the arrangingElements() method
public static void arrangingElements(int a[], int n, int low, int high)
{
//Check whether the "low" is greater than "high"
if(low > high)
{
/*Check whether the "low" is less than array length and "a[low]" is greater than "a[a.length-1]".*/
if(low < a.length && a[low] > a[a.length - 1])
{
/*Assign the "a[low]" value to temporary variable. */
int temp = a[low];
/*Assign the "a[a.length-1]" value to "a[low]". */
a[low] = a[a.length - 1];
//Assign the "temp" value to "a[a.length-1]"
a[a.length - 1] = temp;
}
//Return it
return;
}
/*Otherwise, call arragingElements() method recursively to arrange the elements is less than or equal to "n" before any integer greater than "n". */
else
{
//Check whether the "a[low]" is less than "n"
if(a[low] < n)
//Increment "low" by "1"
low++;
/*Otherwise, check whether "a[low]" is equal to "n". */
else if(a[low] == n)
{
/*Check whether "a[a.length-1]" is not equal to than "n". */
if(a[a.length - 1] != n)
{
/*Assign the "a[low]" value to temporary variable. */
int temp = a[low];
/*Assign the "a[a.length-1]" value to "a[low]". */
a[low] = a[a.length - 1];
/*Assign the "temp" value to "a[a.length-1]"> */
a[a.length - 1] = temp;
}
//Otherwise, increment "low" by "1"
else
low++;
}
//Otherwise, swap the array elements
else
{
/*Assign the "a[low]" value to temporary variable. */
int temp = a[low];
//Assign the "a[high]" value to "a[low]"
a[low] = a[high];
//Assign the "temp" value to "a[high]"
a[high] = temp;
//Decrement "high" by "1"
high--;
}
/*Call arrangingElements() method recursively to find the elements are less than or equal to "n". */
arrangingElements(a, n, low, high);
}
}
Explanation:
In the above code,
- In the arrangingElements() method,
- It accepts the input parameter as “a”, “n”, “low”, and “high”...
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Data Structures and Algorithms in Java
- The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th century. Though the Sierpinski triangle looks complex, it can be generated with a short recursive function. Your main task is to write a recursive function sierpinski() that plots a Sierpinski triangle of order n to standard drawing. Think recursively: sierpinski() should draw one filled equilateral triangle (pointed downwards) and then call itself recursively three times (with an appropriate stopping condition). It should draw 1 filled triangle for n = 1; 4 filled triangles for n = 2; and 13 filled triangles for n = 3; and so forth. API specification. When writing your program, exercise modular design by organizing it into four functions, as specified in the following API: public class Sierpinski { // Height of an equilateral triangle whose sides are of the specified length. public static double height(double length) // Draws a filled equilateral…arrow_forwardImplement a recursive function in Python that will sum of the num- bers in a finite array.arrow_forwardGive a recursive algorithm for the sequential search and explain its running time.arrow_forward
- Give a recursive (decrease-by-one) algorithm for finding the position of the smallest element in an array of n real numbers. and Determine the running time complexity of this algorithm.arrow_forwardWhat is the recursive definition that gives the length of the longest bitonic subsequences in an array? It would be in the form lbs(A)....arrow_forwardUse induction to develop a recursive algorithm for finding theaverage of n real numbers in an array A[1..n]. Analyze its running time.arrow_forward
- Many of the coding interview problems deal with permutations and combinations of a given set of components.1. Backtracking Method: When the subset is added to the resultset, each response is backtracked in this recursive process.An extra array subset's space and time complexity are O(n), respectively (2n). solve the aforementioned issue with Javaarrow_forwardLet A[1...n,1...n] be a 2D array of positive integers with n rows and n columns. Let ArraySum(a,b,c,d) be the sum of all the elements of A[a...b, c...d] (i.e., the submatrix of A with rows a to b and columns c to d). Write down a recursive formula for ArraySum(a,b,c,d) and analyze the time complexity of the corresponding recursive algorithm. You must write a formal recursive formula including the base case and general recursive step.arrow_forwardGive a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b.arrow_forward
- Please Help me with this Problem Given an unsorted array of size n. Array elements are in the range from 1 to n. One number from set {1, 2,..., n} is missing and one number occurs twice in the array. Find these two numbers.arrow_forwardIN JAVA, USING RECURSION PLEASE Create a method int[][] generateMatrix(int row, int col, int boundary1, int boundary2, int iteration) that generates a random matrix with random numbers between [min(boundary1, boundary2), max(boundary1, boundary2)). The sum of the diagonal and the sub-diagonal should be the same. If not, regenerate it again, until a matrix that satisfies the condition is generated (return that matrix). If you try iteration times and none of the matrixes satisfy the condition, return null.arrow_forwardPermutations and Combinations of a given set of components are dealt with in a significant number of coding interview tasks.1. Backtracking Method – In this recursive method, each answer is backtracked after the subset is added to the resultset.Space complexity for an additional array subset is O(n) and time complexity is O(2n). implement above problem in javaarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education