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
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...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT