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.
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).
Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns...
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 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 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]...
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-...
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
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...
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...
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...
1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...
Consider the Java program below to sort an array A in an ascending order. M, N, and K are positive integers, and A is an array of N nonnegative integers where 0 < A[i] < M for all i e {0,..., N -1}. In this program, list is a class of an integer list with the following methods. 1st.size(): returns the number of elements in the list lst 1st.get(i): returns the element at the i-th position in the list lst...