Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 5, Problem 21C

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”...

Blurred answer
Students have asked these similar questions
I need help creating the network diagram and then revising it for the modified activity times.
Activity No. Activity Time (weeks) Immediate Predecessors 1 Requirements collection 3 2 Requirements structuring 4 1 3 Process analysis 3 2 4 Data analysis 3 2 5 Logical design 50 3,4 6 Physical design 5 5 7 Implementation 6 6 c. Using the information from part b, prepare a network diagram. Identify the critical path.
Given the following Extended-BNF grammar of the basic mathematical expressions:  Show the derivation steps for the expression: ( 2 + 3 ) * 6 – 20 / ( 3 + 1 ) Draw the parsing tree of this expression. SEE IMAGE
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning