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.

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

The worst case for this question will be when there exists no x for which -x exists.

In Algorithm 1, for the worst case the time complexity will become , where N is the size of the array because for each element the array will be searched by the linear search algorithm.

In Algorithm 2, it will be because, for each element the array will be searched by the binary search algorithm.

In Algorithm 3, it will take because we are visiting an index only once.

Following code can be used to verify these. (Assuming language C++)

#include <bits/stdc++.h>
using namespace std;

int N;
int arr[1000005];
int ans;
void generateRandomArray(){
for(int i = 0; i < N; i++){
arr[i] = 1 + rand()%10001; // creating worst case array
}
sort(arr, arr+N);
}

int algorithm1(){
for(int i = 0; i < N; i++){
for(int j = i; j < N; j++){
if(arr[i] == -1*arr[j]){
ans = arr[i];
return 1;
}
}
}
return 0;
}

int algorithm2(){
for(int i = 0; i < N; i++){
int x = -1*arr[i];
int low = 0;
int high = N-1;
while(low <= high){
int mid = (low+high)/2;
if(arr[mid] == x){
ans = x;
return 1;
}
if(arr[mid] > x){
high = mid-1;
}
else{
low = mid+1;
}   
}
}
return 0;
}

int algorithm3(){
int i = 0;
int j = N-1;
while(i <= j){
if(arr[i] + arr[j] == 0){
ans = arr[i];
return 1;
}
if(arr[i] + arr[j] < 0){
i++;
}
else{
j--;
}
}
return 0;
}

int main(){
N = 100000;
generateRandomArray();
clock_t start, stop;
start = clock();
int a = algorithm1();
stop = clock();
double time1 = (stop-start)/double(CLOCKS_PER_SEC);

start = clock();
int b = algorithm2();
stop = clock();
double time2 = (stop-start)/double(CLOCKS_PER_SEC);

start = clock();
int c = algorithm3();
stop = clock();
double time3 = (stop-start)/double(CLOCKS_PER_SEC);

cout << "N = " << N << endl;
cout << "Algorithm 1: " << time1 << setprecision(15) << " s" << endl;
cout << "Algorithm 2: " << time2 << setprecision(15) << " s" << endl;
cout << "Algorithm 3: " << time3 << setprecision(15) << " s" << endl;

}

Output:

N = 100000
Algorithm 1: 2.60012 s
Algorithm 2: 0.002045 s
Algorithm 3: 8e-05 s
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