Question

Write Java program to compare time consumed by linear search and binary search to search for non-exit element in arrays of le
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:


elapsed = endTime-startTime; this was missing many times
Updated Code
import java.util.Arrays;
import java.util.Random;
public class Temp {
public static void main(String[] args) {
  
int a1[] = new int[1000];
int a2[] = new int [100000];
int a3[] = new int [500000];
int a4[] = new int[1000000];
long startTime = 0;
long endTime = 0;
long elapsed;
  
  
Random r = new Random();
  
for(int i = 0; i<a1.length; i++)
a1[i]=r.nextInt(1000);
  
for(int i = 0; i<a2.length; i++)
a2[i]=r.nextInt(100000);
  
for(int i = 0; i<a3.length; i++)
a3[i]=r.nextInt(500000);
  
for(int i = 0; i<a4.length; i++)
a4[i]=r.nextInt(10000000);
  
// testing linear search
  
System.out.println("Array with length 1000");
  
startTime = System.nanoTime();
linearSearch(a1, 1100);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Linear search time: ");
System.out.println(elapsed);
  
  
Arrays.sort(a1);
startTime = System.nanoTime();
binarySearch(a1,1100);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Binary search time: ");
System.out.println(elapsed);
  
////////////////////////////////////////////
  
System.out.println(" ");
System.out.println("Array with size 100000");
  
startTime = System.nanoTime();
linearSearch(a2, 110000);
endTime = System.nanoTime();
System.out.print("Linear search time: ");
elapsed = endTime-startTime;
System.out.println(elapsed);
  
Arrays.sort(a2);
startTime = System.nanoTime();
binarySearch(a2,110000);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Binary search time: ");
System.out.println(elapsed);
  
System.out.println(" ");
System.out.println("Array with size 500000");
  
startTime = System.nanoTime();
linearSearch(a3, 550000);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Linear search time: ");
System.out.println(elapsed);
  
Arrays.sort(a3);
startTime = System.nanoTime();
binarySearch(a3,550000);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Binary search time: ");
System.out.println(elapsed);
  
System.out.println(" ");
System.out.println("Array with size 1000000");
  
startTime = System.nanoTime();
linearSearch(a4, 1100000);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Linear search time: ");
System.out.println(elapsed);
  
Arrays.sort(a4);
startTime = System.nanoTime();
binarySearch(a4,1100000);
endTime = System.nanoTime();
elapsed = endTime-startTime;
System.out.print("Binary search time: ");
System.out.println(elapsed);
  
}
  
///// linear search method
public static int linearSearch(int a[], int x) {
  
for(int i = 0; i< a.length; i++) {
if(a[i] == x)
return i;
}
return -1;  
}
  
///// binary search method
  
public static int binarySearch(int a[], int x) {
int lo = 0;
int hi = a.length-1;
int mid;
  
while (lo<=hi) {
mid = (lo+hi)/2;
if(a[mid] == x)
return mid;
else {
if (a[mid] < x)
lo = mid + 1; // search in the second half
else
hi = mid - 1;
}
}
return -1;
}
}

OUTPUT:

Array with length 1000 Linear search time: 66000 Binary search time: 10800 Array with size 100000 Linear search time: 4581800

Add a comment
Know the answer?
Add Answer to:
Write Java program to compare time consumed by linear search and binary search to search for...
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 Java program that will create a random list (array) randomize a  search item to be...

    Write a Java program that will create a random list (array) randomize a  search item to be found in the list sequentially search the array for that item. You must code the search and not use a search function. Display each comparison in the array; the element contents and the index. display whether the item was found or not. Now take that code and alter it to have two lists. Both lists randomize between 1 and 50. While traversing one list,...

  • 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...

  • In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort...

    In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort On the given array with the following elements ; Array : 87, 39, 3, 5 ,9 ,7 ,27,1 , 8 ,6 (use this Array as data for all methods except the BinarySearch method). All your methods will be in a class. Write a tester program that will call each of the listed methods from the class above using the given Array as data. For...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

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