Question

In: Computer Science

Write a Java program with comments that randomly generates an array of 500,000 integers between 0...

Write a Java program with comments 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 executionTime = endTime - startTime;

A. Linear Search

public static int linearSearch(int[] list, int key) {

for (int i = 0; i < list.length; i++) {

if (key == list[i]) return i;

}

return -1;

}

B. Binary Search

public static int binarySearch(int[] list, int key) {

int low = 0;

int high = list.length - 1;

while (high >= low) {

int mid = (low + high) / 2;

if (key < list[mid])

high = mid - 1;

else if (key == list[mid])

return mid;

else

low = mid + 1;

}

return –low - 1; // Now high < low, key not found

}

Solutions

Expert Solution

For the above program, we need to have the following things into consideration.

  1. We need to first use the random class function in order to find the integers for the array.
  2. Then we can search for the middle element for example and then calculate the time accordingly.

Following is the code for the same.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ComparisonOfSearches {
   public static void main(String args[]) {
       Random rand = new Random();
       int list[] = new int[500000];
       for(int i = 0 ;i<500000; i++) {
           list[i] = rand.nextInt()%500000;
       }
       int middleElement = list[250000];
       long start_linear = System.currentTimeMillis();
       int found = linearSearch(list, middleElement);
       long end_linear = System.currentTimeMillis();
       System.out.println("Time Taken by linear search: " + (end_linear - start_linear));
       Arrays.sort(list);
       long start_binary = System.currentTimeMillis();
       found = binarySearch(list, middleElement);
       long end_binary = System.currentTimeMillis();
       System.out.println("Time Taken by binary search: " + (end_binary - start_binary));
   }
   public static int linearSearch(int[] list, int key) {

       for (int i = 0; i < list.length; i++) {

       if (key == list[i]) return i;

       }

       return -1;

   }
   public static int binarySearch(int[] list, int key) {

       int low = 0;

       int high = list.length - 1;

       while (high >= low) {

       int mid = (low + high) / 2;

       if (key < list[mid])

       high = mid - 1;

       else if (key == list[mid])

       return mid;

       else

       low = mid + 1;

       }

       return -1; // Now high < low, key not found

   }
  
}

Following is the output of the above program.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks


Related Solutions

[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and stores them into an array, where n is a parameter of the method. The method returns all the numbers above the average of the n random numbers. Use loops wherever possible. For example, if the 10 random numbers are 44 61 98 45 45 17 63 24 9 95 The method should returns an array which contains 61 98 63 95 In the main...
Write a program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12 and displays the month name Jan, Feb, …, December for the number 1, 2, …, 12, accordingly.. Sample Run: 9 Oct 2- Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi Riyals. Prompt the user to enter 0 to convert from U.S. dollars to Saudi Riyals and 1 to convert from Saudi Riyal and...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
6. Write a Java program to rotate an array (length 3) of integers in left direction...
6. Write a Java program to rotate an array (length 3) of integers in left direction 7. Write a Java program to store a collection of integers, sort them in ascending order and print the sorted integers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT