
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![Consider the following implementation of binarySearch:
public static bool binarySearch(int[] a, int item) {
int first = 0;
int last = a.length - 1;
while( first <= last) {
int mid = (first + last)/2;
if(a[mid]
item) return true;
else if (a[mid] < item) first = mid + 1;
else
last = mid - 1;
}
return false;
==
}
how many times is the line of code int mid = (first + last)/2; executed when it is
called with a = {2, 3, 5, 7, 11, 13,17, 19, 23, 29, 31, 37} and item = 2?](https://content.bartleby.com/qna-images/question/d1507ef8-c387-4f95-9ab7-d384c0c17d2a/fce40d3b-c63c-4c53-abc4-f4679f3a37db/kfar4dp_thumbnail.png)
Transcribed Image Text:Consider the following implementation of binarySearch:
public static bool binarySearch(int[] a, int item) {
int first = 0;
int last = a.length - 1;
while( first <= last) {
int mid = (first + last)/2;
if(a[mid]
item) return true;
else if (a[mid] < item) first = mid + 1;
else
last = mid - 1;
}
return false;
==
}
how many times is the line of code int mid = (first + last)/2; executed when it is
called with a = {2, 3, 5, 7, 11, 13,17, 19, 23, 29, 31, 37} and item = 2?
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- What will the final values of count1 and count2 be in terms of n? assume that n is a power of 2 of the form 2^r.arrow_forwardQuestion 3 Consider the following code: public static boolean[] [] whichOdd (int[] [] arr) { boolean[] [] result = new boolean [arr.length] [arr [0].length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { result[i][j] = arr[i][j] % 2 != 0; } [ ] Suppose you have written this solution to a problem that asks you to write a method that accepts a 2D array of integers (guaranteed to be rectangular) and returns a 2D array of boolean s where each element is true if its corresponding element in the parameter 2D array is odd, and false otherwise. You've already tried it with the problem's example which uses 2D array: } [ J return result; and after it's passed to your method, it correctly contains: [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] O [true, false, true, false], [true, false, true, false], [true, false, true, false], [true, false, true, false] Select an option below that makes a good test candidate to test a different case that shows…arrow_forwardWhat is wrong with my Java code? public static void main(String[] args) { class IncrementClass<T> { T value; } IncrementClass<String> i = new IncrementClass<>(); int[] intArray = new int[10]; for(i.value = 0; i.value<intArray.length;i.value++){ intArray[i.value]=i.value; System.out.print(intArray[i.value]); } }arrow_forward
- Write all the code within the main method in the Test Truck class below. Implement the following functionality. a) Constructs two truck objects: one with any make and model you choose and the second object with gas tank capacity 10. b) If an exception occurs, print the stack trace. c) Prints both truck objects that were constructed. import java.lang.IllegalArgumentException ; public class TestTruck { public static void main ( String [] args ) { heres the truck class information A Truck can be described as having a make (string), model (string), gas tank capacity (double), and whether it has a manual transmission (or not). Include the following methods in your class definition. . An overloaded constructor which takes the make and model. This method throws an IllegalArgumentException if the make is "Jeep". An overloaded constructor which takes the gas tank capacity. This method throws an IllegalArgumentException if the capacity of the gas…arrow_forwardimport java.util.Comparator; import java.util.PriorityQueue; class StringLengthComparator implements Comparator public int compare(String o1, String 02) if (01.length()02.length()){ return 1; }else{ } } } } class StringLengthComparator1 implements Comparator { return 0; public int compare(String o1, String o2) { if(01.length()02.length()){ return -1;//means here we swap it }else{ } } return 0; public class StringLength { public static void main(String[] args) { String arr[] = {"this", "at", "a", "their", "queues"}; StringLengthComparator string Comparator = new StringLengthComparator(); StringLengthComparator1 string Comparator1 = new StringLengthComparator1(); PriorityQueue pq = new PriorityQueue(stringComparator); PriorityQueue pq1 = new PriorityQueue(stringComparator1); for(int i=0;iarrow_forwardJAVA Language: Transpose Rotate. Question: Modify Transpose.encode() so that it uses a rotation instead of a reversal. That is, a word like “hello” should be encoded as “ohell” with a rotation of one character. (Hint: use a loop to append the letters into a new string) import java.util.*; public class TestTranspose { public static void main(String[] args) { String plain = "this is the secret message"; // Here's the message Transpose transpose = new Transpose(); String secret = transpose.encrypt(plain); System.out.println("\n ********* Transpose Cipher Encryption *********"); System.out.println("PlainText: " + plain); // Display the results System.out.println("Encrypted: " + secret); System.out.println("Decrypted: " + transpose.decrypt(secret));// Decrypt } } abstract class Cipher { public String encrypt(String s) { StringBuffer result = new…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education