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

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

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

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

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

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

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
ADVERTISEMENT