Question

In: Computer Science

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 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

Answer:

(a)#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)
{

build_maxheap(array,n);
int j = n-1,k;
while(j>0)
{

k=array[0];
array[0]=array[j];
array[j]=k;
  
j--;
  
}
  
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;
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:

Screenshot of the code:


Related Solutions

(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 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
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
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...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Please do Part III Part I Problem statement: Given an array of 'n' non-duplicate integers, find...
Please do Part III Part I Problem statement: Given an array of 'n' non-duplicate integers, find pairs of integers from the array that make the sum equal to K. Eg. [1, 2, 7, 11, 6, 9, 5] K= 12 Soln: (1,11), (7,5). Part II Write an Oracle for testing this program using Data Flow Testing. As usual, write down test cases in a tabular form with reasons, inputs, expected output etc. Part III   1.       Identify the basic blocks in your...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT