Question

Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns...

Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns the index i of array a[0..n-1] where a[i]=x. Obviously, the result is bs(a,n,x)=x, and the binary search function can be tested using the loop

for(j=0; j<K; j++)
for(i=0; i<n; i++) if(bs(a,n,i) != i) cout << “\nERROR”;

Select the largest n your software can support and then K so that this loop with an iterative version of bs runs 3 seconds or more. Then measure and compare this run time and the run time of the loop that uses a recursive version of bs. Compare these run times using maximum compiler optimization (release version) and the slowest version (minimum optimization or the debug version). If you have a desktop machine, use it. If you must use a laptop, make measurements using AC power, and then same measurements using only the battery. What conclusions can you derive from these experiments? Who is faster? Why? What is the time for executing a single bs program? If you have different compilers you can compare them.

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


import java.util.*;
public class Main
{
  
int binarySearch1(int arr[], int l, int r, int x) //Binary search using recursion
{
if (r >= l) {
int mid = l + (r - l) / 2;
  
  
if (arr[mid] == x) .// if the mid element is the search elemnent return mid;
return mid;
  
if (arr[mid] > x)
return binarySearch1(arr, l, mid - 1, x); //if x is on the right side search on right side.
  
  
return binarySearch1(arr, mid + 1, r, x); // if x is on left side search on left side.
}
  
  
  
return -1;
}
  
int binarySearch2(int arr[], int x) //Binary search using Iteration
{
  
int l = 0, r = arr.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
  

if (arr[mid] == x)  // if the mid element is the search elemnent return mid;
return mid;
  

if (arr[mid] < x) // if x is on left side search on left side.
l = mid + 1;
  
  
else
r = mid - 1;  //if x is on the right side search on right side.
}
  

return -1;
  
}
   public static void main(String[] args) {
   Scanner scan=new Scanner(System.in);
   System.out.println("Enter the elemnts of the array"); //taking elemnts of array
   int n=scan.nextInt();
       int arr[]= new int[n];
      
       for(int i=0;i<n;i++)
       arr[i]=scan.nextInt();
      
       System.out.println("Enter the number you want to search");
       int k=scan.nextInt();
      
       Main ob = new Main();
      
       long start1 = System.nanoTime(); //systems current time in nano seconds
      
       int p=ob.binarySearch1(arr,0,n-1,k);
       long end1 = System.nanoTime(); //systems current time in nano seconds
long microseconds1 = (end1 - start1) / 1000; //measuring time elapsed for recusive binary search
      
       //long startTime2 = System.currentTimeMillis();
      
      
       long start2 = System.nanoTime();   //systems current time in nano seconds
       int q= ob.binarySearch2(arr,k);
      
   long end2 = System.nanoTime();   //systems current time in nano seconds
long microseconds2 = (end2 - start2) / 1000; //measuring the time elapsed for iterative binary search
      
      
      
       System.out.println(microseconds1); //Printing the time elapsed for both the methods
       System.out.println(microseconds2);
   }
}

Explanation- Since in recursive binary search there will always be a overhead of manipulating call stack the time taken by recursive binary search is a little more than iterative binary search. Though the time complexity of both the algorithm is O(nLogn).

Add a comment
Know the answer?
Add Answer to:
Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns...
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
  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Consider an ordered array A of size n and the following ternary search algorithm for finding...

    Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...

  • Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]:...

    Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]: ascending integers) k =1 j =n loop when (k is less than j) m =floor((k+j)/2) if (target is larger than the element at m) then k = m+1 else j = m endloop if (target equals element at k) then location=k else location =0

  • An array A of n integers is called semi-sorted if it is increasing until some index...

    An array A of n integers is called semi-sorted if it is increasing until some index and then decreasing afterwards. In other words, there is some index 1 ≤ p ≤ n such that: • A[i] < A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i < n. Note that in this case, A[p] is the maximum element of A. Give an algorithm with running time O(logn) that finds...

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

  • Refer to the following program sample to answer the parts (a-i) below. Keep in mind that low and high are both indices of array A. /1 Assume that A is a sorted array containing N integers // Assume t...

    Refer to the following program sample to answer the parts (a-i) below. Keep in mind that low and high are both indices of array A. /1 Assume that A is a sorted array containing N integers // Assume that x is a variable of type int int low= 0; // Line 1 int high- N; // Line 2 while (low- high) I/ Line 3 m- (lowthigh)/2; // Line 4 (This is integer division) if (A [m]<) I/Line 5 then low-...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#....

    Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#. This is my first program in C# and I translated it from Java. In this program, I am to carry out the same 10,000,000 unsuccessful searches for eight different-sized arrays, namely arrays of sizes, 128, 512, 2048, 8192, 32768, 131072, 524288, and 2,097,152. I need to measure each of the three programs and the time it takes to do the 10,000,000 searches for each...

  • 1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can...

    1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can select more than one option A) It uses a Variable-Size Decrease-and-Conquer design technique B) Its average case time complexity is Θ(log n) C) Its worst case time complexity is Θ(n) D) It can be implemented iteratively or recursively E) None of the above 2. Randomized Binary Search: Example Assume you have an array, indexed from 0 to 9, with the numbers 1 4 9...

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