Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Consider both of the following approaches to the binary search: Approach A private static int search (int[] a, int searchValue) { int left =

1. Consider both of the following approaches to the binary search:

Approach A

private static int search (int[] a, int searchValue) {

int left = 0 int right = a.length1

while (left <= right) {

int midpoint = (left+right)/2

if (a[midpoint] == searchValue) return midpoint

else if (a[midpoint] < searchValue)

left = midpoint + 1

else right = midpoint 1 }

return 1 }

Approach B

private static int search (int[] a, int target, int left, int right) {

if (left > right) return 1

else { int midpoint = (left + right) / 2

if (a[midpoint] == target) return midpoint

else if (a[midpoint] < target)

return search (a, target, midpoint + 1, right)

else

return search (a, target, left, midpoint 1)

}

}

Which approach uses recursion? Explain why Approach B has more parameters than Approach A.

2.import java.util.*;

public class Foothill {

public static void main (String[] args) {

ArrayDeque accountsPayable = new ArrayDeque();

accountsPayable.add(155.1F);

accountsPayable.add(328.2F);

accountsPayable.add(35.3F);

accountsPayable.add(415.4F);

for (int k = 0; k < 5; k++)

if ( !accountsPayable.isEmpty() )

System.out.print( accountsPayable.remove() + " ");

else System.out.print("All accounts paid");

System.out.println(); } }

This program uses the ArrayDeque like which data structure? stack or queue?

3.You need a generic collection class that will insert items in order using a comparator method because you need to have a sorted collection to do a binary search. You decide that inserting items in sorted order is a better strategy than sorting the collection later. Which generic collection class should you choose?

A. LinkedList

B. Queue

C. PriorityQueue

D. ArrayList

4. A binary search of a pre-sorted array of 256 elements would take (at most) how many comparisons? (Assume the search key is the key on which the array is sorted).

5. Consider this method definition:

static int someRecMethod( int n ) {

if ( n < 0 ) return -1;

return someRecMethod( n + 1 );

}

what is wrong with the above code?

Thanks!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions