Question

Unsure how to convert this setup into a linkedList setup while still using key in java...

Unsure how to convert this setup into a linkedList setup while still using key in java

private int size = n;

public boolean find(int key) {

     int low = 0, high = size-1;

     while (low <= high) {

          int mid = (low + high) /2;

          if (set[mid] == key) return true;

          if (key > set[mid] low = mid + 1;

          else high = mid - 1;

     }

     return false;

}

Homework Answers

Answer #1

CODE

import java.util.LinkedList;

import java.util.List;

public class Main {

private List<Integer> set = new LinkedList<Integer>();

public boolean find(int key) {

int low = 0, high = set.size()-1;

while (low <= high) {

int mid = (low + high) /2;

if (set.get(mid) == key) return true;

if (key > set.get(mid))

low = mid + 1;

else high = mid - 1;

}

return false;

}

}

NOTE: In order to run this method, first you would need to initialize the elements in set

Know the answer?
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
Similar Questions
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...
int bsearch(int[] arr, int key) { int lo = 0, mid, hi = arr.length-1; while (...
int bsearch(int[] arr, int key) { int lo = 0, mid, hi = arr.length-1; while ( lo <= hi ) { mid = (lo + hi) / 2; if ( key < arr[mid] ) hi = mid – 1; else if ( arr[mid] < key ) lo = mid + 1; else return mid; } return -1; } Count the number of operations (comparisons and assignments), find an original function with an input n, and give an analysis of the...
Draw a program flow graph for the function below int binsearch(int x,int v[],int n) { int...
Draw a program flow graph for the function below int binsearch(int x,int v[],int n) { int low,high,mid; low=0; high=n-1; while(low<high) { mid = ( low + high ) / 2; if( x < v[mid]) high = mid - 1; else if ( x > v[mid]) low = mid + 1; else return mid; } return -1; }
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
This is my course class. Can someone please explain what the equals method is for and...
This is my course class. Can someone please explain what the equals method is for and also the compareTo method is for in general and then explain what the methods are doing in this class. public class Course {    private boolean isGraduateCourse;    private int courseNum;    private String courseDept;    private int numCredits;       public Course(boolean isGraduateCourse, int courseNum, String courseDept, int numCredits) {        this.isGraduateCourse=isGraduateCourse;        this.courseNum=courseNum;        this.courseDept=courseDept;        this.numCredits=numCredits;   ...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
how can i convert the while loop into a for loop? string input;     cin >> input;...
how can i convert the while loop into a for loop? string input;     cin >> input;     cout << endl;     int count = 0;     int size = input.size(); if(input.size() == 6) {         while(size > 0) {             if(count < 3) {                 if((input.at(count) >= 'A' && input.at(count) <= 'Z') || (input.at(count) >= 'a' && input.at(count) <= 'z')) {                     count++;                 }                 else {                     cout << "error: letter expected at position " << count + 1;                     return 0;                 }             }             else if(input.at(count) >= '0' &&...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT