Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
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
Given an unsorted array, A, of integers and an integer k, write a recursivejava code for rearranging the elements in A so that all elements less than or equal to k come before any elements larger than k. What is the running time of your algorithm on an array of n values.
By using java, Write a recursive function that finds the maximum element in an array ofintegers, based on comparing the first element in the array against themaximum in the rest of the array recursively.
Write a recursive function using python for the problem: A palindrome is a sequence of characters which is the same when the sequence is reversed. Given an array A indexed from p to q containing characters, determine if the sequence of characters in A[p..q] forms a palindrome. Return True is a palindrome is formed and return False if it does not.
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education