Question

Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

Using Java solve recursively without the use of loops or modifying the method signatures.

/**

* Given a sorted input array a, return a sorted array of size a.length + 1,

* consisting of all elements of array a and integer i.

*

* @param a an array that is sorted in a non-descending order

* @param i an integer

* @return a sorted array of size a.length + 1, consisting of all elements of

* array a and integer i. e.g., insertIntoSortedArray({1, 2, 4, 5}, 3)

* returns a sorted array {1, 2, 3, 4, 5}.

*

* <b>You are forbidden to use the insertIntoSortedList method below to

* solve this problem.</b>

*

* Requirement: You are required to implement all methods recursively.

* You receive a zero if there is any occurrence of a loop (e.g., for,

* while).

*/

public int[] insertIntoSortedArray(int[] a, int i) {

/*

* Your Task: Define a recursive method yourself and use it here.

*/

return null;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.IOException;

public class InsertIntoSortedArrayUsingRecursion {

public static int[] insertIntoSortedArray(int[] a, int i) {

int [] arr=new int[a.length+1];

insert(arr, a, i ,0, false);

for(Integer an: arr) {

System.out.print(an+" ");

}

return arr;

}

public static void insert(int[] arr,int[] a, int i, int index, boolean found) {

if(index == arr.length -1) {

if(found) {

return ;

}else {

arr[index] = i;

return;

}

}

int newValue = index;

if( i <= a[index] && !found) {

arr[index] = i;

found = true;

}else {

if(found) {

arr[index+1] = a[index];

}else {

arr[index] = a[index];

}

newValue++;

}

insert(arr, a, i ,newValue, found);

}

public static void main(String[] args) throws IOException {

int[] a = {1, 2,4, 5};

insertIntoSortedArray(a, 3);

}

}

output is:

Add a comment
Know the answer?
Add Answer to:
Using Java solve recursively without the use of loops or modifying the method signatures. /** *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

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

  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

  • c++ I have to write the following functions that I will later use for testt but...

    c++ I have to write the following functions that I will later use for testt but you just have to write the following functions again I have written the interface of it and please make sure it works. will have an array of randomly generated numbers - An array that is already sorted in ascending order - An array that is sorted in reverse (descending) order - An array that is sorted except for the last 10 numbers which are...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • I am using Java to try and solve this problem. Suggestions on where I am going...

    I am using Java to try and solve this problem. Suggestions on where I am going wrong? PREVNEXT Workbench ? a Exercise 20662- WORK AREA RESULTS Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int. x9 of 9: 2018-07-10 12:58:15 -W SUBMIT 1 public int sumArray(int[] a) 2 int i; int sum = 0; for (í 0; í?a.length; ?++)...

  • Question 10 15 pts In this question, you are asked to implement a recursive function that...

    Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sorted arrays of integers are disjoint or not (two arrays are disjoint iff they share no common elements). Your function needs to follow the following signature: int is Disjoint (int" sorted 1, int size 1, int* sorted 2, int size2) Here is the description of the input and output parameters of is Disjoint: • int" sorted 1: pointer to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT