Question

In: Computer Science

find the k-th smallest value given array[ 5, 2, 1, 15, 6, 9, 3, 4, 11]...

find the k-th smallest value

given array[ 5, 2, 1, 15, 6, 9, 3, 4, 11] , k=2, the algorithm return the first two value: 1, 2

please show steps of how to output the first 2 values by using merge sort

and please analyze the running time.

your time is greatly appreciated.

Solutions

Expert Solution

It is request to please don't leave without thumbs up I really need it so that I can provide quality answer to you hoping for good thank you! And please don't dislike ask query in comment section

As from the given question it is ask for kth smallest element but in the output you want the k smallest element in the array so as per output of the program i provide the souce code of k smallest number

but as your question is not full or specific as it ask for merge sort if you have any doubts so please ask in comment section

so time complexity of the program is very simple as i have sort the program using sort function which uses merge sort to sorting the element is O(nlogn) and there is another loop which run for k times so time Complexity of that loop is O(k) so total time Complexity of the program is t(n)= O(nlogn) +O(k)

I am attaching the output scnshot of the program which is exactly you want in the question

// C++ code for k largest elements in an array
#include <bits/stdc++.h>

using namespace std;


void kSmallest(int arr[], int n, int k)
{

    // Sort the given array arr


    sort(arr, arr + n);


    // this will print  first k smallest elements

    for (int i = 0; i < k; i++)

        cout << arr[i] << " ";
}

// driver program

int main()
{

    int n,k;
    cout<<"enter the size of array\n ";
    cin>>n;
    cout<<endl;
    cout<<"enter the k value  ";
    cin>>k;
    cout<<endl;
    int arr[1000];
    cout<<"enter the element of the array \n";
    for(int i = 0; i<n; i++)
    cin>>arr[i];

    cout<<"k smallest element are: "<<kSmallest(arr, n, k);
}

Output:

And please upvote it is request THANKYOU!


Related Solutions

Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number
Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number (use the last element in each sequence as pivot). Show the intermediate steps (the result of each recursive step including the pivot, k’s value and grouping).
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5 8 1 12 14 Each column represents a different treatment given to sick rats. Each cell is a different rat. Use statistical analysis and use post hoc testing using contrasts to find the best treatment. Treatment 1: vitamins Treatment 2: prescription pills Treatment 3: brain surgery Treatment 4: shock therapy Treatment 5: dietary changes
Given the matrix 7 7 -4 12 -5 A = 9 10 2 6 13 8 11 15 4 1 3. a. Sort each column and store the result in an array B.
Given the matrix a. Sort each column and store the result in an array B.b. Sort each row and store the result in an array C.c. Add each column and store the result in an array D.d. Add each row and store the result in an array E.  
In python please :) How to get the n th smallest even value of given array?...
In python please :) How to get the n th smallest even value of given array? (Use for loop for this problem. Do not use existing codes. Use your own codes). For example, in given list [11,23,58,31,56,22,43,12,65,19], if n is defined as 3. The program will print 56. (By using for loop and obtain the set of evens. By using another for loop, from the set of evens remove (n-1) observations and break the loop and find the minimum observation...
Day 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...
Day 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Number of Aides Absent 5 8 11 15 4 2 7 1 4 6 14 19 3 5 8 In which of the following ranges you can find the Upper Control Limit of the control chart? 0.1427 0.1536 0.1677 Not computable with information available In which of the following ranges you can find the Lower Control Limit of the control chart? Does not exit...
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Test score 67 67 87 89 87 77 73 74 68 72 58 98 98 70 77 Above we have the final averages of the last stats and I want to know if the class average falls within the boundaries of all my statistics classes for the past 20 years. Find the sample size, mean, and standard deviation of the data above (Table 1)....
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3 5 4 7 9 7 0 0 4 5 6 9 9 8 1 3 5 6 8 9 9 0 1 2 3 5 9 The table represent a random sample of 31 test scores taken from a large lecture class. Find the following [round to 2 decimal points X. XX] a) [2 pts] Find the 5 number summary [L, Q1, Q2, Q3,...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5 4; 9 8 7] Note: TA2 is defined to be a linear transformation that maps any vector x to A2* x. That is TA2 = A2*x. Also the range of the Linear transformation represented by A2 is the same as the column space of A2. l) Find a basis for the null(TA2). m) Find nullity of A2, TA2 and A2tA2. n) Find rank(A2), rank(A2t),...
Week 1 2 3 4 5 6 Value 18 14 15 10 17 15 Using the...
Week 1 2 3 4 5 6 Value 18 14 15 10 17 15 Using the naïve method (most recent value) as the forecast for the next week, compute the following measures of forecast accuracy. (a) Mean absolute error If required, round your answer to one decimal place. (b) Mean squared error If required, round your answer to one decimal place. (c) Mean absolute percentage error If required, round your intermediate calculations and final answer to two decimal places. (d)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT