Question

1) Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint....

1) Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes.

a. The value is found and the array index of that value is returned.

b. The value is not found and we return -1.

2) Convert the pseudocode in (1) above into a working Java program.

Homework Answers

Answer #1

If you need any correction/clarifications kindly comment

Please give a Thumps Up if you like the answer.

PseudoCode

Main

Step 1: Start

Step 2:Declare and initialize input array.

Step 3: Declare and initialize the search key.

Step 4: Call the recursive function index=recSearch(a, 0, a.length-1, key)

Step 5:If index!=1, then the value is found . Otherwise, The value is not found .

Step 6: Stop

Function recSearch(int arr[], int l, int r, int x)
    Step 1:    if (r < l)     return -1
    Step 2: if (arr[l] == x)     return l
    Step 3: if (arr[r] == x)       return r
    Step 4: Otherwise    call recursive function recSearch(arr, l+1, r-1, x);
    Step 5: Stop

Program

import java.util.Scanner;

public class Main
{
public static void main(String args[])
   {
      int i,key=9;
      int a[]={1,4,7,9,10};   
      int index = recSearch(a, 0, a.length-1, key);
      if (index != -1)
           System.out.println("Element " + key + " is present at index " +
                                                    index);
        else
            System.out.println("Element " + key + " is not present");
   }
public static int recSearch(int arr[], int l, int r, int x)
     {
          if (r < l)
             return -1;
          if (arr[l] == x)
             return l;
          if (arr[r] == x)
             return r;
          return recSearch(arr, l+1, r-1, x);
     }
}

Output

Element 9 is present at index 3

Know the answer?
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
Similar Questions
Task 1 Write algorithms in pseudocode to implement an array. The following operations must be performed...
Task 1 Write algorithms in pseudocode to implement an array. The following operations must be performed on the array: *Add an element *Insert an element at a given position x. *Retrieve/Read the value of an element at position y. *Delete an element from position z. *Clear all values from the array. (x,y, and z represent any value in the valid range of indices of the array).
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given N distinct integer points in 1-dimension. That is, all points are integers on the x-axis, and are in an array. This algorithm should go through all pairs (without sorting the elements of the array), one pair at a time, to find the current closest pair. For example, assume we had the following integers: 4, 2, -2, -1 in an array. We would then generate...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write a function to check if a machine uses little endian or big endian notation. write...
Write a function to check if a machine uses little endian or big endian notation. write the function checkEndianess(), complete the main function implementation to print the value returned by the function and print if the machine is “Little Endian” or “Big Endian”, compile and run the program. The function checkEndianess() should return the following values: • 0 if the architecture is "Little Endian" • 1 if the architecture is "Big Endian". #include int main() { int check; // Variable:...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT