From Algorithms 4th Ed by Sedgewick, Robert. Ex 1.4.10(modified)
Exercise 1.4.10
I’m modifying the exercise from what’s in the book. I want two functions, lower_bound() and upper_bound(), defined as:
int lower_bound(int[] a, int key)
int upper_bound(int[] a, int key)
lower_bound() should return the index of the first (lowest indexed) element of a that is greater than or equal to key, and upper_bound() returns the index of the first (lowest indexed) element of a that is greater than key. You may assume the array is sorted – take that as a hint that something like a binary search is needed. If every element of a is less than the key, both upper_bound and lower_bound return the length of a. If every element of a is less than or equal to key, upper_bound returns the length of a.
Here are some examples:
a = { 1, 1, 2, 2, 4, 4, 4, 5, 6, 6 }
lower_bound(a,0) returns 0 upper_bound(a,0) returns 0
lower_bound(a,3) returns 4 upper_bound(a,3) returns 4
lower_bound(a,2) returns 2 upper_bound(a,2) returns 4
lower_bound(a,4) returns 4 upper_bound(a,4) returns 7
lower_bound(a,6) returns 8 upper_bound(a,6) returns 10
lower_bound(a,7) returns 10 upper_bound(a,7) returns 10
Both algorithms must run in log(N) time. This means you cannot simply use the book’s binary search and then increment backward and forward as needed, since that could take time proportional to N in the worst case. (For example, consider looking for a 1 in an array with a million 1s. Binary search would immediately return the middle index, and then lower_bound and upper_bound would each run through a half million elements looking for the first and last matches.)
public class Bounds { static int lower_bound(int[] a, int key){ int left = 0; int right = a.length - 1; int mid; while(left != right){ if(left == right - 1){ if(a[left] >= key) return left; else if(a[right] >= key) return right; else return a.length; } mid = (left + right) / 2; if(a[mid] < key){ left = mid; } else{ right = mid; } } if(a[left] >= key) return left; else return a.length; } static int upper_bound(int[] a, int key){ int left = 0; int right = a.length - 1; int mid; while(left != right){ if(left == right - 1){ if(a[left] > key) return left; else if(a[right] > key) return right; else return a.length; } mid = (left + right) / 2; if(a[mid] <= key){ left = mid; } else{ right = mid; } } if(a[left] > key) return left; else return a.length; } public static void main(String[] args){ int[] a = { 1, 1, 2, 2, 4, 4, 4, 5, 6, 6 }; System.out.println("lower_bound(a, 0): " + lower_bound(a, 0)); System.out.println("lower_bound(a, 3): " + lower_bound(a, 3)); System.out.println("lower_bound(a, 2): " + lower_bound(a, 2)); System.out.println("lower_bound(a, 4): " + lower_bound(a, 4)); System.out.println("lower_bound(a, 6): " + lower_bound(a, 6)); System.out.println("lower_bound(a, 7): " + lower_bound(a, 7)); System.out.println("upper_bound(a, 0): " + upper_bound(a, 0)); System.out.println("upper_bound(a, 3): " + upper_bound(a, 3)); System.out.println("upper_bound(a, 2): " + upper_bound(a, 2)); System.out.println("upper_bound(a, 4): " + upper_bound(a, 4)); System.out.println("upper_bound(a, 6): " + upper_bound(a, 6)); System.out.println("upper_bound(a, 7): " + upper_bound(a, 7)); } }
lower_bound(a, 0): 0
lower_bound(a, 3): 4
lower_bound(a, 2): 2
lower_bound(a, 4): 4
lower_bound(a, 6): 8
lower_bound(a, 7): 10
upper_bound(a, 0): 0
upper_bound(a, 3): 4
upper_bound(a, 2): 4
upper_bound(a, 4): 7
upper_bound(a, 6): 10
upper_bound(a, 7): 10
From Algorithms 4th Ed by Sedgewick, Robert. Ex 1.4.10(modified) Exercise 1.4.10 I’m modifying the exercise from...
4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...
6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of the string. (b) An array originally contained different numbers in ascending order but may have been subsequently rotated by a few positions. For example, the resulting array may be: 21 34 55 1 2 3 4 5 8 13 Is it possible to adapt the Binary Search...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
Question 3 Searching Algorithms [12 marks (4 marks) Define a method that when passed an array of integers arr and another integer target, returns the last index in arr where target exists and -1 if target isn't found in arr a. b. (4 marks) Provide a trace, in the form of a logic table, of running the following method for 1, 4, 4, 4, 5, 7, 8, 12, 12, 15, 16, 17, 35, 35, 35, 40 arr = target 14...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain only the statement return −1? Assume the first JUnit test for the findLargest method described in Self-Check exercise 2 in section 3.4 is a test that determines whether the first item in a one element array is the largest. What would be the minimal code for a method findLargest that passed this test? PROGRAMMING Write the findLargest method described in self-check exercise 2 in...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...