Question

In: Computer Science

Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...

Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches:

a. heapify A and then extract k elements one by one

b. sort the array (e.g. using MergeSort or HeapSort) and then read the first k elements

c. apply the selection procedure k times to find the 1st, 2nd, · · · , the k’th smallest element;

d. apply the selection procedure to find the k’th smallest element, then use that element to find the k smallest elements, and then sort those k elements.

For each approach, find the asymptotic worst-case running time which should be of the form Θ(f(n, k)).

Now assume k = √ n. Then for each approach express the asymptotic running-time as a function of n only.

e. Implement HeapSort. Write the code in Python IDE, run some test results and discuss if your results are supporting the claim of Θ(n lg n).

f. How does the heapsort perform compared to MergeSort and QuickSort

Solutions

Expert Solution

#include <iostream>
using namespace std;

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
   int largest = i; // Initialize largest as root
   int l = 2 * i + 1; // left = 2*i + 1
   int r = 2 * i + 2; // right = 2*i + 2

   // If left child is larger than root
   if (l < n && arr[l] > arr[largest])
       largest = l;

   // If right child is larger than largest so far
   if (r < n && arr[r] > arr[largest])
       largest = r;

   // If largest is not root
   if (largest != i) {
       swap(arr[i], arr[largest]);

       // Recursively heapify the affected sub-tree
       heapify(arr, n, largest);
   }
}

// main function to do heap sort
void heapSort(int arr[], int n,int k)
{
   // Build heap (rearrange array)
   for (int i = n / 2 - 1; i >= 0; i--)
       heapify(arr, n, i);

   // One by one extract an element from heap
   for (int i = n - 1; i >= 0; i--) {
       // Move current root to end
       swap(arr[0], arr[i]);

       // call max heapify on the reduced heap
       heapify(arr, i, 0);
   }
}

/* A utility function to print array of size n */
void printArray(int arr[], int k)
{
   for (int i = 0; i < k; ++i)
       cout << arr[i] << " ";
   cout << "\n";
}

// Driver program
int main()
{
   int arr[] = { 5, 4, 6, 2, 10 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int k=4;
   heapSort(arr, n, k);

   cout << "Sorted array is \n";
   printArray(arr, k);
}


Related Solutions

Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
. Let xj , j = 1, . . . n be n distinct values. Let...
. Let xj , j = 1, . . . n be n distinct values. Let yj be any n values. Let p(x) = c1 + c2x + c3x 2 + · · · + cn x ^n−1 be the unique polynomial that interpolates the data (xj , yj ), j = 1, . . . , n (Vandermonde approach). (a) Remember that (xj , yj ), j = 1, . . . , n are given. Derive the n...
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such...
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such that, in any two columns, each ordered pair of symbols occurs exactly once. Prove that there exists an OA(k, n) if and only if there exist (k - 2) mutually orthogonal Latin squares of order n. (combinatorics and design)
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm? Java Language....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm?
In how many ways n distinct balls can be given to k children so that no...
In how many ways n distinct balls can be given to k children so that no child gets more than 3 balls?
a) In how many ways n distinct ball can be given to k children so that...
a) In how many ways n distinct ball can be given to k children so that no child gets more than 3 balls? b) What happens if the balls are indistinguishable?
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT