Question

In: Computer Science

We can find and sort the k largest elements of a set of size n in...

We can find and sort the k largest elements of a set of size n in Θ(n + k log k) worst-case time.

Is this statement true?

Solutions

Expert Solution

Hello Reader,

Making the long story short, Yes We can find and sort the k largest element of a set of n elements in Θ(n+k log k) time.

To be more precise the exact time would be something: Θ( (n-k)*k + k log k )

1.) The time required to find the k largest element would be K*(n-K).

2.) The time required to sort the k elements using merge sort would be K log K

Algorithm:

1.) Take the array or set of size n.

2.) Take the elements form 1-k into some temporary array t1, remaining k+1 - n elements into temporary array t2.

3.) Find the minimum element in array t1 and compare it with each element of t2, if min < any element of t2 then swap the elements in both the array.

4.) Again check for the minimum element in t1 and repeat the above step k times.

So the time to find the K largest element is Θ((k-n)*k) or Θ(n).

Then sorting is simple business i.e Apply merge sort which is Θ(n log n) but we apply it in only the array t1 which has k elements so Θ(k log k).

Thus we conclude that Θ(n + k log k ) is possible.

Hope you like the arguments above.

Any suggestion and questions are welcomed in the comment box.

Thanks and regards and keep loving us!!!!!!!!!


Related Solutions

Let f(n,k) be the number of equivalence relations with k classes on set with n elements....
Let f(n,k) be the number of equivalence relations with k classes on set with n elements. a) What is f(2,4)? b) what is f(4,2)? c) Give a combinational proof that f(n,k) = f(n-1,k-1)+k * f(n-1,k)
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.
Finding the complexity of Finding the largest two elements in a queue of size n+3 using...
Finding the complexity of Finding the largest two elements in a queue of size n+3 using Naïve search. with explain
What is the largest size n of a problem that can solve in 1 second, assuming...
What is the largest size n of a problem that can solve in 1 second, assuming that the algorithm to solve the problem takes sqrt(n) microseconds? (sqrt stands for square root) 1 10^(6) 10^(12) 10^(3) How does the array arr = [8, 5, 3, 6, 10] look like after the second iteration (when j = 3) of the j loop on line 1 of the give pseudocode below? insertion_sort.PNG 1 for j 2 do 3 // Insert A[j] into the...
Let A be a set with m elements and B a set of n elements, where...
Let A be a set with m elements and B a set of n elements, where m; n are positive integers. Find the number of one-to-one functions from A to B.
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an input of n numbers stored in an array A[1:n]. Compare the following strategies in terms of their exact number of comparisons. Strategy 1: adapt the two separate For loops idea for minimum and maximum. Strategy 2: Run mergesort to sort the numbers in ascending or descending order, then output the 2nd ranked and (n-1)th ranked elements. First write each algorithm in pseudocde, then analyze...
Suppose we find that, the average engine size in our sample of size n = 30...
Suppose we find that, the average engine size in our sample of size n = 30 is 192 cubic inches, with a standard deviation of 104 cubic inches. Use these statistics to compute a 90% confidence interval of population mean, that is, the average engine size for all.
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
K-NN and Perceptron theory question If we don’t care about the size of the TRAINING SET...
K-NN and Perceptron theory question If we don’t care about the size of the TRAINING SET or TIME to classify which Machine Learning algorithm would you choose 1-NN or perceptron. Explain your answer or provide specific examples. what are the pros and cons of perceptron and 1-NN beside time and number of inputs. please provide specific example of what kinds data are best for each algorithms
Let A be a finite set. Say A has five elements. (a) Can you find a...
Let A be a finite set. Say A has five elements. (a) Can you find a function g : A → A which is injective but not surjective? Explain your answer. (b) Can you find a function f : A → A which is surjective but not injective? Explain your answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT