Question

In: Computer Science

Write a decrease-conquer algorithm to solve the following problem: input: a nonempty unsorted array A[lo..hi] of...

Write a decrease-conquer algorithm to solve the following problem:

input: a nonempty unsorted array A[lo..hi] of n distinct nonnegative integers;

output: the (left) median value of A[lo..hi].

What is the asymptotic running time of your algorithm ?

Solutions

Expert Solution

Let the algorithm be known as decrease-conquer(A,lo,hi) which takes three inputs one is array , lo is its lower index as given and hi is higher index as given. median is nothing but (n/2)th smallest element in sorted array.

let hi - lo + 1 = n.

C style psuedoCode:-

decrease-conquer(A,lo,hi)//suppose it takes T(n)

{

if(lo==hi)// it takes c1 time

return A[lo]// it takes c2 time

else

{

m = partition(A,lo,hi) // it takes O(n) time

if(m = (hi-lo + 1) / 2) // // it takes c3 time

return A[m]

else

if(m>(hi-lo + 1) / 2)// it takes c4 time

decrease-conquer(A,lo,m-1)// it takes T(m-lo) time

else

decrease-conquer(A,m+1,hi)// it takes T(hi-m) time

}

}

partition(A,lo,hi)

{

x = A[lo]//pivot

i = lo

j = i+1

while(j<=hi)//as long as j is smaller or equal to hi

{

if(A[j] <= x)

{

i++

swap(A[i] , A[j])

j++

}

}

}

Analysis:-

let hi - lo + 1 = n.

Clearly , partition involves only 1 while loop of length n and rest are just constants time seeking so, time taken by partition algorithm = O(n).

Recurrence relation:-

T(n) = c1 + c2 = O(1), if n = 1

= c1 + c3 + c4 + T(m-lo) + O(n) = T(m-lo) + O(n) , if n>1

or

=c1 + c3 + c4 + T(hi-m) + O(n) = T(hi-m) + O(n) , if n>1

so , in average case or best case:-

m - lo = hi-m = almost equal to (n/2)

so , T(n) = T(n/2) + n

solving using subsitution we get

= T(n/22) + n/2 + n

= T(n/23) + n/4 + n/2 + n

continuing like this k times we get

= T(n/2k) + n( 1/2k-1 + .... + 1/2 + 1)

it will stop when n/2k =1 , k = log2n

note (1/2k-1 + .... + 1/2 + 1 ) is decreasing geometric progression . and always decreasing geometric progression = O(1))

so , T(n) = T(1) + n * O(1) = O(n)

Therefore , in average and best case T(n) = O(n)

Worst case:-

Here input array will be almost sorted or such that partition always keeps either left or right constant in number .

so , recurrence relation here reduces to T(n) = T(n-1) + n

T(n) = T(n-1) +   n

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

= T(n-3) + n-2 + n-1 + n

k times we get

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

it will stop when n-k = 1 so , k = n-1

= 1 + 2 + 3 ....+n

= n(n+1)/2 = O(n2)

Therefore in worst case T(n) = O(n2)


Related Solutions

Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem: input: an array A[lo..hi]...
Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem: input: an array A[lo..hi] of n real values; output: true iff the array contains two elements (at different indices) whose sum is 2020.
Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array...
Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array A[lo..hi] of n real numbers. Requirement: Shouldn't use a sorting algorithm. Complexity O(nlgn)
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Describe a polynomial time algorithm to solve following problem Input: A boolean function in CNF such...
Describe a polynomial time algorithm to solve following problem Input: A boolean function in CNF such that each clause has exactly three literals. Output: An assignment of the variables such that each clause has all TRUE literals or all FALSE literals.
Think of an algorithm to find the maximum value in an (unsorted) array. Now, think of...
Think of an algorithm to find the maximum value in an (unsorted) array. Now, think of an algorithm to find the second largest value in the array. Which is harder to implement? Which takes more time to run (as measured by the number of comparisons performed)? Now, think of an algorithm to find the third largest value. Finally, think of an algorithm to find the middle value. Which is the most difficult of these problems to solve?
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.
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT