Question

Write a java program: Create a method fillRandom() that accepts an array of int as input...

Write a java program:

Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys)

Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10 columns line up - that is, specify a minimum field width of five.

In your main() method declare int arrays of size 1000, 100,000 and two arrays of 10,000,000. Call fillRandom() to populate the first three arrays. Populate the last array with ints from 50,000 to -49,999 in reverse order.

For each array:

call displayLastInts(), pause five seconds, call overloaded library method sort() on the array, call displayLastInts() on the sorted array.

Pause five seconds before proceeding to the next array.

Surround the calls to sort() with code that will record the system time before and after each sort.

Call library method binarySearch() three times on each sorted array. Search first for 0, then for 900, then for 2000.

Surround the calls to binarySearch() with code that will record the system time before and after each search.

Calculate the time elapsed for each operation and output a user friendly summary.

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

import java.util.*;
public class RandomArrays{
    static void fillRandom(int[] arr){
        int size = arr.length;
        for (int i=0; i<size; i++){
            Random rand = new Random();
            int n = rand.nextInt(2000) - 999;
        }
        arr[0] = 0;
        arr[1] = 900;
    }

    static void displayLastInts(int[] arr){
        int size = arr.length;
        int n = 0;
        while(n<100){
            if (n%10==0 && n!=0){
                System.out.print("\n");
            }
            System.out.print(arr[size-100+n]+"\t");
            n++;
        }
    }

    public static void main(String[] args) {
        int[] arr1 = new int[1000];
        int[] arr2 = new int[100000];
        int[] arr3 = new int[10000000];
        int[] arr4 = new int[100000];
        fillRandom(arr1);
        fillRandom(arr2);
        fillRandom(arr3);
        for (int i=0; i<arr4.length; i++){
            arr4[i] = 50000 - i;
        }
        try{
            displayLastInts(arr1);
            Thread.sleep(5000);
            Arrays.sort(arr1);
            displayLastInts(arr1);
            Thread.sleep(5000);
            displayLastInts(arr2);
            Thread.sleep(5000);
            Arrays.sort(arr2);
            displayLastInts(arr2);
            Thread.sleep(5000);
            displayLastInts(arr3);
            Thread.sleep(5000);
            Arrays.sort(arr3);
            displayLastInts(arr3);
            Thread.sleep(5000);
            displayLastInts(arr4);
            Thread.sleep(5000);
            Arrays.sort(arr4);
            displayLastInts(arr4);
            long startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 0);
            long endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a java program: Create a method fillRandom() that accepts an array of int as input...
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
  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • In java, write method, shuffle, which accepts an array of int, creates a copy of the...

    In java, write method, shuffle, which accepts an array of int, creates a copy of the formal parameter, shuffles the copy, then returns the copied, shuffled array, leaving the formal parameter unchanged. Shuffling is a process of swapping each element of array with another element randomly chosen from the array. For testing the shuffle method have main call shuffle, repeatedly having it shuffle its previously returned array until the returned (shuffled) array is identical (equal) to the original array that...

  • Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...

    Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1]      return 2 a = [ 0,0,0,1]          return 3 a = [0,0,0]              return -1 int indexFirstOne...

  • 1.You are to write a program name search.java that will do the following: 2.You are to...

    1.You are to write a program name search.java that will do the following: 2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C. 3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99. 4.Write 1 sequential search...

  • Write Java program to compare time consumed by linear search and binary search to search for...

    Write Java program to compare time consumed by linear search and binary search to search for non-exit element in arrays of length 1000, 100000,500000,1000000. Hints: - Use Random class, method nextInt(a.length) to generate random numbers. - Select an element that is greater than a.length. - Use Arrays.sort(a) to sort the array before using binarySearch.

  • DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input...

    DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input array is sorted and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst case runtime must be O(logn) where n is the number of elements. Example a = [0, 0, 1, 1, 1] return 2

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

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
ADVERTISEMENT