8.12 LAB: Binary search Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an argument. Complete the recursive method binarySearch() with the following specifications: Parameters: a target integer an ArrayList of integers lower and upper bounds within which the recursive call will search Return value: the index within the ArrayList where the target is located -1 if target is not found The template provides main() and a helper function that reads an ArrayList from input. The algorithm begins by choosing an index midway between the lower and upper bounds. If target == integers.get(index) return index If lower == upper, return -1 to indicate not found Otherwise call the function recursively on half the ArrayList parameter: If integers.get(index) < target, search the ArrayList from index + 1 to upper If integers.get(index) > target, search the ArrayList from lower to index - 1 The ArrayList must be ordered, but duplicates are allowed. Once the search algorithm works correctly, add the following to binarySearch(): Count the number of calls to binarySearch(). Count the number of times when the target is compared to an element of the ArrayList. Note: lower == upper should not be counted. Hint: Use a static variable to count calls and comparisons. The input of the program consists of: the number of integers in the ArrayList the integers in the ArrayList the target to be located Starter code for LabProgram.java import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class LabProgram { // Read and return an ArrayList of integers. private static ArrayList readNums(Scanner scnr) { int size = scnr.nextInt(); // Read size of ArrayList ArrayList nums = new ArrayList(); for (int i = 0; i < size; ++i) { // Read the numbers nums.add(scnr.nextInt()); } return nums; } static public int binarySearch(int target, ArrayList integers, int lower, int upper) { /* Type your code here. */ } public static void main(String [] args) { Scanner scnr = new Scanner(System.in); // Input a list of integers ArrayList integers = readNums(scnr); // Input a target value for the search int target = scnr.nextInt(); int index = binarySearch(target, integers, 0, integers.size() - 1); System.out.printf("index: %d, recursions: %d, comparisons: %d\n", index, recursions, comparisons); } }
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
8.12 LAB: Binary search
Binary search can be implemented as a recursive
Complete the recursive method binarySearch() with the following specifications:
- Parameters:
- a target integer
- an ArrayList of integers
- lower and upper bounds within which the recursive call will search
- Return value:
- the index within the ArrayList where the target is located
- -1 if target is not found
The template provides main() and a helper function that reads an ArrayList from input.
The algorithm begins by choosing an index midway between the lower and upper bounds.
- If target == integers.get(index) return index
- If lower == upper, return -1 to indicate not found
- Otherwise call the function recursively on half the ArrayList parameter:
- If integers.get(index) < target, search the ArrayList from index + 1 to upper
- If integers.get(index) > target, search the ArrayList from lower to index - 1
The ArrayList must be ordered, but duplicates are allowed.
Once the search algorithm works correctly, add the following to binarySearch():
- Count the number of calls to binarySearch().
- Count the number of times when the target is compared to an element of the ArrayList. Note: lower == upper should not be counted.
Hint: Use a static variable to count calls and comparisons.
The input of the program consists of:
- the number of integers in the ArrayList
- the integers in the ArrayList
- the target to be located
Starter code for LabProgram.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class LabProgram {
// Read and return an ArrayList of integers.
private static ArrayList<Integer> readNums(Scanner scnr) {
int size = scnr.nextInt(); // Read size of ArrayList
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < size; ++i) { // Read the numbers
nums.add(scnr.nextInt());
}
return nums;
}
static public int binarySearch(int target, ArrayList<Integer> integers,
int lower, int upper) {
/* Type your code here. */
}
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
// Input a list of integers
ArrayList<Integer> integers = readNums(scnr);
// Input a target value for the search
int target = scnr.nextInt();
int index = binarySearch(target, integers, 0, integers.size() - 1);
System.out.printf("index: %d, recursions: %d, comparisons: %d\n",
index, recursions, comparisons);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images