Question

Write a recursive function that computes the smallest integer in a given array of integers. Use...

Write a recursive function that computes the smallest integer in a given array of integers. Use the following algorithm:

int min(int[] A, int low, int high) {

if (low == high) return A[low];

int mid = (low + high) / 2;

int min1 = min(A, low, mid);

int min2 = min(A, mid + 1, high);

if (min1 > min2) return min2;

return min1; }

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

public class FindMinimum {

public static int min(int[] A, int low, int high) {

if (low == high) return A[low];

int mid = (low + high) / 2;

int min1 = min(A, low, mid);

int min2 = min(A, mid + 1, high);

if (min1 > min2) return min2;

return min1;

}

public static void main(String[] args) {

int arr[] = {3,4,1,10,6,4,5,6,7,8,9,100,2};

System.out.println(min(arr,0,arr.length - 1));

}

}

===================================
SEE OUTPUT

86 public static int min(int A, int low, int high) { 87 88 if (low high) return A[low]; 89 90 int mid (lowhigh) / 2; 91 92 in

PLEASE COMMENT if there is any concern.

==============================

Add a comment
Know the answer?
Add Answer to:
Write a recursive function that computes the smallest integer in a given array of integers. Use...
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
  • 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...

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • How to write a recursive method named lessThanKFirst that receives an array of integers a and...

    How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a  = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • *Program is in C* Write a recursive function to compute a^b for integers a and b....

    *Program is in C* Write a recursive function to compute a^b for integers a and b. For the recursive use the following equality a^b = a^b - 1 * a and a^0 = 1. What does the following recursive function do? int mystery(int a, int b) {if (b == 1) return (a); else return (a + mystery(a, b - 1));}

  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • Write a function that returns the index of the smallest element in an array of integers....

    Write a function that returns the index of the smallest element in an array of integers. If there are more such elements than one, return the smallest index. C programming

  • Write a recursive function in C++ that displays all nodes data (integers) in an array of...

    Write a recursive function in C++ that displays all nodes data (integers) in an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this is determined by another part of the program) }

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

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