Question

In: Computer Science

Consider the following algorithm to find the kth largest elementof a given array A of...

Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.

Answer the following questions.

Write the above procedure as a small pseudocode.

Is the algorithm correct? Justify your answer.

Express the worst case run time of this procedure by writing a recurrence relation.

Solve the recurrence relation.

Solutions

Expert Solution

Yes The above algorithm is correct and in-fact it the best algorithm to find Kth largest element in an array in linear time

Algorithm is

1.Divide the array into n/5 groups where size of each group is 5 elements

2.Sort all the above [n/5] created groups and find median of all groups , Also create a median array which needs to store the medians of all n/5 groups

3.Recursively call to find median of medians i.e medofMedian

4.Apply partition algorithm taking result of step 4 as pivot .

pos = partition(arr, n, medOfMedian)

5) If pos == k return medOfMedian
6) If pos > k return kthSmallest(arr[l..pos-1], k)
7) If pos < k return kthSmallest(arr[pos+1..r], k-pos+l-1)

Now pseudocode for above algorithm is

int kthSmallest(int arr[], int s, int l, int k)

{

    // If k is smaller than number of elements in array

    if (k > 0 && k <= l - s+ 1)

    {

        int n = l-s+1; // Number of elements in arr[l..r]

    // Divide arr[] in groups of size 5, calculate median

        // of every group and store it in median[] array.

        int i, median[(n+4)/5];

        for (i=0; i

            median[i] = findMedian(arr+s+i*5, 5);

        if (i*5 < n) //For last group with less than 5 elements

        {

            median[i] = findMedian(arr+s+i*5, n%5);

            i++;

        }    

   // Find median of all medians using recursive call.

        int medOfMedian = (i == 1)? median[i-1]:

                                 kthSmallest(median, 0, i-1, i/2);

// Partition the array around a random element and

        // get position of pivot element in sorted array

        int pos = partition(arr, s, l, medOfMedian);

   // If position is same as k

        if (pos-s == k-1)

            return arr[pos];

        if (pos-1 > k-1) // If position is more, recur for left

            return kthSmallest(arr, s, pos-1, k);

// Else recur for right sub array

        return kthSmallest(arr, pos+1, l, k-pos+s-1);

     // If k is more than number of elements in array

    return INT_MAX;

}

Time complexity taken by this algorithm

Time Taken each of the Five steps

Step 1

Dividing the n elements into n/5 group will take constant time i.eO(1)

Step 2

To find median of n elements we need O(n ) time

Sort the above created ⌈n/5⌉ groups and find median of all groups. Create an auxiliary array ‘median[]’ and store medians of all ⌈n/5⌉ groups in this median array.

So here we have groups of only 5 elements so time needed is constant i.e O(1)

so In all Step 1 and Step 2 will take O(n) time

Step 3

We recursively call to find median of median

so Time taken = T(n/5)

Step 4

Is normal Partition algorithm that takes O(n) time

Step 5

What is the worst case size of these recursive calls?

The answer is maximum number of elements greater than median (obtained in step 3) or maximum number of elements smaller than median

At least half of the medians found in step 2 are greater than or equal to median. Thus, at least half of the n/5 groups contribute 3 elements that are greater than median, except for the one group that has fewer than 5 elements. Therefore, the number of elements greater than median is at least.


So In the worst case, the function recurs for at most n – (3n/10 – 6) which is 7n/10 + 6 elements.

So time needed is T(7n/10 +6) = T(7n+10)

So total Recurrence Relation is

T(n) = T(n/5) + T(7n/10) + O(n)

Solve this using substitution

T(n)  <= cn/5 + c(7n/10 + 6) + O(n)
     <= cn/5 + 7cn/10 + c + 6c + O(n)
    <= 9cn/10 + 7c + O(n)
    <= cn, 

So we get on solving the recursion relation time complexity as O(n) is worst case

This completes the algorithm with code and time complexity


Related Solutions

Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
(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...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
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 write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the...
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the array after each pass. Also calculate how many comparisons you will be required to pass
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (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...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT