Question

In: Computer Science

Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5...

Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5 exists in an array would work. Your explanation should include:

- a description of your algorithm using psuedocode
- a proof of correctness of your algorithm
- an analysis of the runtime of the algorithm

Solutions

Expert Solution

A Divide and Conquer algorithm to search for an element in an unsorted array can be done by dividing the problem into smaller problems by size one less than the previous problem i.e we first compare the first element of the array of size 'n' with 5, if it is not equal then we recursively search for 5 in rest of the array whose size is now 'n-1'.

1) PSEUDOCODE:

divide_conquer(A, low, high, x) // In our case x=5 and A is the array where the element is to be searched and low is the lower index of A and high is the higher index of A

{

if (high < low)

{

return NOT_FOUND;

}

if (A[low] == x)

return FOUND;

else

return divide_conquer(A, low+1, high, x);

}

2) CORRECTNESS:

The algorithm is correct because if low in the first recursive call is equal to the key then we found it at first and this is the best case and if not we are searching the rest of the array for the key. Every recursive call just decreases the size of the array by 1 to search the key. Thus, ultimately we are searching every element of the array for the key.

3) TIME COMPLEXITY:

The recurrence relation can be written as:-

T(n) = T(n-1) + c, where T(n) is the time taken to search for the key in an array of size n.

T(0) = c, base case.

Now, if we substitute T(n-1) we get:-

T(n) = T(n-2) + c + c

Now, if we substitute T(n-2) we get:-

T(n) = T(n-3) + 3c

If we do this 'k' times we get:-

T(n) = T(n-k) + kc.

Now let n-k = 0.

or, k=n.

As T(0) =c, if we substitute k=n in the equation we get:-

T(n) = c(n+1)

Hence T(n) = O(n).


Related Solutions

Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of...
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of the array have equal values. For simplicity, suppose the array size is n=2k for some integer k. Input S is the starting index, and n is the number of elements starting at S. The initial call is SAME(A, 0, n). Boolean SAME (int A[ ], int S, int n) { Boolean T1, T2, T3; if (n == 1) return TRUE; T1 = SAME (A,...
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
The following submission rules apply:·    For those questions requiring programs, the solutions must be implemented using JavaScript or Java.o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.o Solutions must be provided in plain text so that formatting is not lost.·    All answers must be provided in this document.·    Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.Design and analyze a divide-and-conquer algorithm for finding the maximum...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity. Please provide a solution in Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT