Question

In: Computer Science

For the problem below, please estimate the worst-case Running Time for a random array of size...

For the problem below, please estimate the worst-case Running Time for a random array of size n, and prove that it is indeed ( ). Please show all your work. Just stating something like "since 2 Binary Search runs in ( ) time, our algorithm has the same runtime estimate" is not enough. A rigorous explanation is expected.

import java.util.*;
public class Main
{
static int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
  
// If the element is present at the middle itself
if (arr[mid] == x)
return mid;
  
// If element is smaller than mid, then it can only
// be present in left subarray
if (arr[mid] > x)
{ int rr= binarySearch(arr, l, mid - 1, x);
if(rr==-1)//modified
{
return binarySearch(arr, mid + 1, r, x);
}
return rr;
}
// Else the element can only be present in right
// subarray
int k =binarySearch(arr, mid + 1, r, x);
//modified part
if(k==-1)
{
return binarySearch(arr, l, mid - 1, x);
}
return k;
}
  
// We reach here when element is not present in array
return -1;
}
  
public static int FindIndex(int[] arr, int x)
{
if(arr.length==0)return -1;//if array is empty
return binarySearch(arr,0,arr.length-1,x);
  
}
   public static void main(String[] args) {
   int a[] = {3, 17, 28, 935, 1011, -10, 0, 2} ;//declaring array
   int r = FindIndex(a,935);
       System.out.println("index of 935:"+r);
       r = FindIndex(a,-10);
       System.out.println("index of -10:"+r);
      
       r = FindIndex(a,0);
       System.out.println("index of 0:"+r);
      
       r = FindIndex(a,1);
       System.out.println("index of 1:"+r);
      
       r = FindIndex(a,3);
       System.out.println("index of 3:"+r);
      
       r = FindIndex(a,2);
       System.out.println("index of 2:"+r);
      
       r = FindIndex(a,17);
       System.out.println("index of 17:"+r);
   }
}

Solutions

Expert Solution

Please find the STEP-BY-STEP SOLUTION Below.


Related Solutions

Please state the worst case run time for the following with an example of the worst...
Please state the worst case run time for the following with an example of the worst case and explain why! 1. Dijksta's Algorithm 2. Bellman-Ford Algorithm 3.DAG Algorithm 4. Prim's Algorithm 5. Kruskal's Algorithm 6. Baruvka's algorithm
(C++)Order statistics: Write codes for Rand-Select (with linear expected running time) and Select (with linear worst-case...
(C++)Order statistics: Write codes for Rand-Select (with linear expected running time) and Select (with linear worst-case running time). Test your two programs with an input array that is a random permutation of A = {1, 2, 3, …, 99, 100}
Please enter a seed: 1 Please enter the size of the array: 1 Array size must...
Please enter a seed: 1 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 12 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements are less than half of the first element...
6. The worst case scenario in the quick sort occurs when the array is partitioned to...
6. The worst case scenario in the quick sort occurs when the array is partitioned to two equal sized subarray every time that a recursive call takes place. True False 7.Suppose that we want to sort an array of n elements, where each element is a string of at most 1000 characters. What is the time requirement for applying radix sort to sort this array? O(n2) O(1000n) O(l000logn) O(nlogn) 8.Suppose we want to sort the following array of integers using...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements {5, 4, 3, 2, 1} Determine a formula that counts the numbers of nodes in that recursion tree. What is the Big O for execution time. Determine a formula that expresses the height of the recursion tree. What is the Big O for memory?
Show that the worst-case and average-case time complexities for the number of assignments of records performed...
Show that the worst-case and average-case time complexities for the number of assignments of records performed by the Exchange Sort algorithm (Algorithm 1.3) are given by           W(n) = 3n(n-1)/2 ≈ n2/2 and A(n) = 3n(n-1)/4 ≈ n2/4
What is the running time of QUICKSORT on an array A of length n containing all...
What is the running time of QUICKSORT on an array A of length n containing all the same values? What is the running time if A contains distinct elements sorted in decreasing order? Explain your answers in detail, referring to the source code for PARTITION and QUICKSORT.
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
(a) Estimate the asymptotic running time of searching in a skewed BST and a balanced BST....
(a) Estimate the asymptotic running time of searching in a skewed BST and a balanced BST. Fill the following table with the big-O running times of each case. skewed tree balanced tree search (b) Explain each of the running times in the table applying the rules of counting seen in class. Specify which method(s) of the BST class you will use, if we have covered them already in class, directly refer to the running times of those methods. (Partial credit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT