Question

In: Computer Science

(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 of size about 10.

(b) What does whatDoIDo do? Explain your answer.

(c) What is the worst-case running time of whatDoIDo, in dependence of the size N of the given array? Explain your answer.

Solutions

Expert Solution

solution:

#include<iostream>
using namespace std;
void max_heapify(int *array, int i, int n)
{
int j, temp;
temp = array[i];
j = 2 * i;
while (j <= n)
{
if (j < n && array[j+1] > array[j])
j = j + 1;
if (temp > array[j])
break;
else if (temp <= array[j])
{
array[j / 2] = array[j];
j = 2 * j;
}
}
array[j/2] = temp;
return;
}
void build_maxheap(int *array,int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(array,i,n-1);
}
}
void print_array(int *a,int n)
{
   int i=0;
   cout<<"-------------\n";
   while(i<n)
   {
       cout<<*(a+i)<<" ";  
       i++;
   }
   cout<<"\n------------------\n";
  
}
void whatDoIDo(int *array,int n)
{
   //step1 build heap..
   build_maxheap(array,n);
//   print_array(array,n);
   int j = n-1,k;
   while(j>0)
   {
       //swapping
       k=array[0];
       array[0]=array[j];
       array[j]=k;
          
       j--;//decrement..  
          
   }
  
   print_array(array,n);
  
}
int main()
{
   int n=10;
   int a[n],b[n],c[n];
   cout<<"Enter 10 elements for array 1:";
   int i=0;
   //reading 3 arrays...
   while(i<10)
   {
       cin>>a[i];  
       i++;
   }
  
   cout<<"Enter 10 elements for array 2:";
   i=0;
   while(i<10)
   {
       cin>>b[i];
       i++;  
   }
  
   cout<<"Enter 10 elements for array 3:";
   i=0;
   while(i<10)
   {
       cin>>c[i];  
       i++;
   }
  
  
   whatDoIDo(a,n);
   whatDoIDo(b,n);
   whatDoIDo(c,n);
  
   //c) worst case running time : O(n^2 logn)
  
  
  
   //21 22 23 24 25 26 27 28 29 30
   //11 12 13 14 15 16 17 18 19 20
   //1 2 3 4 5 6 7 8 9 10
   return 0;
}

output:-

Enter 10 elements for array 1:1 2 3 4 5 6 7 8 9 10
Enter 10 elements for array 2:11 12 13 14 15 16 17 18 19 20
Enter 10 elements for array 3:21 22 23 24 25 26 27 28 29 30
-------------
10 9 8 5 6 7 4 3 2 1
------------------
-------------
20 19 18 15 16 17 14 13 12 11
------------------
-------------
30 29 28 25 26 27 24 23 22 21
------------------


Process exited normally.
Press any key to continue . . .


Related Solutions

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...
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
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...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2,...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2, 7, 7, 5}, N = 5 Output: Duplicate Element: 5 Duplicate Element: 7 Example 2 Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8 Output: Duplicate Element: 2 Duplicate Element: 5 Duplicate Element: 6 CODE:: class Main {    public static void FindDuplicate(int[] arr){ // write your code to find duplicates here and print those values    }   ...
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...
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT