Question

Ex. Some of the codes on the next page indicate the alignment and search of the...

Ex. Some of the codes on the next page indicate the alignment and search of the float and double arrays using the sort and binarySearch methods of the array class.

1)Create an array of float and double numbers by creating 3,000 random integers and dividing them by 300, creating a program to sort them out and to search for specific values (and also output time to perform using StopWatch).

2)Write a program that searches for the 10th smallest number and the 200th smallest number.

3)Write a program to search for the median, print out the performance time, and compare it to the time by binarySearch.

Write java code and show me the output

Thank you :)

If you can't understand the question, let me know. :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package HomeworkLib1;

import java.util.Arrays;

public class SearchTime {

        public static void main(String[] args) {
                double array[] = new double[3000];                              //Creating array
                int i=0;
                for( ; i<3000; i++) {
                        int x = (int)(Math.random()*3000);                      //Creating random integer betwee 0 and 3000
                        array[i] = x/300.0;                                                     //dividing by 300
                }
                System.out.println("Time taken to sort the array (in nanoseconds): " + sortTime(array));
                System.out.println("Search Time: " + searchTime(array, 6.3333));
                System.out.println("Search Time: " + searchTime(array, 1.0));
                System.out.println("Search Time: " + searchTime(array, 10.264));
                System.out.println("Search Time: " + searchTime(array, 7.66663333));
                System.out.println("Median search Time: " + median(array));
                System.out.println("10th smallest element is " + tenthSmallest(array));
                System.out.println("200th smallest element is " + twoHundredthSmallest(array));         
        }

        public static long sortTime(double[] arr) {
                long startTime=0;
                long stopTime=0;
                startTime = System.nanoTime();
                Arrays.sort(arr);
                stopTime = System.nanoTime();
                return stopTime-startTime;
        }
        public static long searchTime(double[] arr, double key) {
                long startTime=0;
                long stopTime=0;
                int x = 0;
                startTime = System.nanoTime();
                x = Arrays.binarySearch(arr, key);
                stopTime = System.nanoTime();
                if(x>=0)
                        System.out.print(key + " Found ");
                else
                        System.out.print(key + " Not Found ");
                return stopTime-startTime;              
        }
        public static double tenthSmallest(double[] arr) {                      //Since array is already sorted
                return arr[9];
        }
        public static double twoHundredthSmallest(double[] arr) {
                return arr[199];
        }
        public static double median(double[] arr) {
                //median is the mid element
                long startTime=0;
                long stopTime=0;
                startTime = System.nanoTime();
                int x = (int)Math.ceil(arr.length/2);
                
                double y = arr[x];
                stopTime = System.nanoTime();
                System.out.println("Median is " + y + " ");
                return stopTime - startTime;
        }
}

Add a comment
Know the answer?
Add Answer to:
Ex. Some of the codes on the next page indicate the alignment and search of the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT