Question

Suppose you have a sorted array of positive and negative integers and would like to determine...

Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the array. Algorithm #3: Maintain two indices i and j, initialized to the first and last element in the array, respectively. If the two elements being indexed sum to 0, then x has been found. Otherwise, if the sum is smaller then 0, advance i; if the sum is larger then 0 retreat j, and repeatedly test the sum until either x is found, or i and j meet. Determine the running times of each algorithm, and implement all three obtaining actual timing data for various values of N. Confirm your analysis with the timing data. (Language in Java)

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

Run time of first algorithm is O(n2). Outer loop will run for n times worst case and inner loop will also run n times.

Run time of second algorithm is O(n log(n)). Outer loop will run for n times worst case in binarySearch1 method and inner loop of binary search1 will run log(n) times.

Run time of third algorithm is O(n). There's only one loop here which will run over the array once if element is not found.

Code for the three algorithms with 3 different functions are calculated for given array,calling one function each time and commenting others to jot down the timings of runtime.

public class SortCheck {

   public static int linerSearch(int[] arr){
      
       int size = arr.length;
       for(int j=0;j<size;j++)
       {
           int key =-arr[j];
           for(int i=0;i<size;i++){
               if(arr[i] == key){
                   return i;
               }
           }
       }
       return -1;
   }
   public static int binarySearch(int[] arr){
      
       int size = arr.length;
       int value = -1;
       for(int j=0;j<size;j++)
       {
           int key =-arr[j];
           value = binarySearch1(arr,0,size-1,key);
           if(value>-1)
               return value;
       }
       return value;
      
   }
   public static int binarySearch1(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;

if (arr[mid] > x)
return binarySearch1(arr, l, mid - 1, x);
  
return binarySearch1(arr, mid + 1, r, x);
}
  
// when element is not present control reaches here
return -1;
}
  
   public static int thirdAlgo(int[] arr){
  
   int size = arr.length,i=0,j=size-1;
  
   while(i!=j)
   {
       int sum = arr[i]+arr[j];
       if(sum ==0)
           return i;
       else if(sum >0)
           j--;
       else
           i++;
   }
   return -1;
   }
  
  
   public static void main(String a[]){
       final long startTime = System.nanoTime();
       int[] arr1= {-23,-17,-4, -3,-2,1,4,5,7,8,14,25};
       //System.out.println("Element found at "+linerSearch(arr1));
       //System.out.println("Element found at "+binarySearch(arr1));
       System.out.println("Element found at "+thirdAlgo(arr1));
       final long duration = System.nanoTime() - startTime;
       System.out.println(duration);
   }
}

Sample output for 3rd algorithm

Running time tabulated for given array above

Algorithm Running time(ns)
Linear Search 583661
Binary Search 488943
Third algo 369055
Add a comment
Know the answer?
Add Answer to:
Suppose you have a sorted array of positive and negative integers and would like to determine...
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
  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Suppose you are given an array A holding n distinct integers (negative values are allowed) in...

    Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include a) Statement of your algorithm in plain English. (Pseudo-code is optional.) b)...

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • We have a sorted array of length n which consists of both positive and negative numbers....

    We have a sorted array of length n which consists of both positive and negative numbers. Now we square each of the numbers in the array. Describe an algorithm that sorts the new squared array within O(n) time.

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

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

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

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