Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. Write the following Java Method (your code should probably be under twenty lines): Public static int firstnonsmallerindex (int[] array, int value) The method takes

a. Write the following Java Method (your code should probably be under twenty lines): Public static int firstnonsmallerindex (int[] array, int value) The method takes in an array in sorted orader and a value, and returns the smallest possible index of an element that is equal to or larger than the given value (or -1 if the value is larger than the max). Your method must run in O(logN) time provided the list has few duplicates. Assuming array = {1,2,3,3,3,4,5,5,14,17}, here are some example calls: method call return value(should be an output) Firstnonsmallerindex(array,3) 2 Firstnonsmallerindex(array,4) 5 Firstnonsmallerindex(array,-1) 0 Firstnonsmallerindex(array,23) -1 Firstnonsmallerindex(array,15) 9 b. After implementing your method in java and ensuring that it is correct, run timing tests on your method with arrays of different sizes. Use the method createrandomsortedarray(shown later) and System.nonoTime() (see http://docs.oracle.com/javase/6/docs/api/java/lang/System.html) (nano time %28%29) to help you create random sorted arrays and run your timing tests. Answer the following questions: What array sizes did you choose and why? What were the runtimes of each array size? Did your runtimes increase as you expected according to the big-oh of your algorithm? Why or why not? For part a, your firstnonsmallerindex method should be found in a class named firstnonsmaller and should be saved in a file named firstnonsmaller.java . Turn in firstnonsmaller.java electronically by submitting it to the turn in link on the homework webpage. For part b, you can save the code/methods you use to do your timing tests in firstnonsmaller.java- we will not grade your timing code. However, we will grade your answers to the questions above. Save your answers in a file named README.txt and also submit that file using the same turn in link on the homework webpage.. Import java.util.*; .. Public static int[] createrandomsortedarray (int size) { random rand = new random(); int[] array = new int[size]; for(int i=0; i

please answer both of the questions and take array run time

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+associated with political parties and if so, which ones? Are

Answered: 1 week ago