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

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
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.
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position,...
Explain 5 incentives which influences how households on aggregate divide their income between consumption and savings
Explain 5 incentives which influences how households on aggregate divide their income between consumption and savings
The following algorithm is widely used for checking whether a credit or debit card number has...
The following algorithm is widely used for checking whether a credit or debit card number has been entered correctly on a website. It doesn't guarantee that the credit card number belongs to a valid card, but it rules out numbers which are definitely not valid. Here are the steps: Check that the long number has exactly 16 digits. If not, the long number is not valid. If the long number has 16 digits, drop the last digit from the long...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT