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

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

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

  • Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array...

    Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array named partNumbers that you used for task 3. Sort the array in ascending order. 1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, and 1078. Java: use Array.sort() C#: use Array.Sort() PHP: use sort() Write code that asks the user to enter two part numbers. For C#, use console input. Implement a binary tree search function called binarySearch() to search the array for the...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a Java program named BSTree.java that will: 1) Generate 20 random integer numbers ranging from...

    Write a Java program named BSTree.java that will: 1) Generate 20 random integer numbers ranging from 1-99. 2) Build a Binary Search Tree using this set of numbers. Write the add method. 3) After building the tree, display the data into three formats: prefix order, infix order, and postfix order. 4) Write a method to delete an element from the Binary Search Tree. First search the item in your TREE and then delete it.

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