Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Work with priority queues. Description: Solve the \"Selection Problem

ID: 3913981 • Letter: O

Question

Objective: Work with priority queues. Description: Solve the "Selection Problem" described in the chapter 6 slides using Algorithm 2 (slide 31). You may use the BinaryHeap class from the textbook. Your program should take a command-line parameter for specifying which item to find, for example, 3 means find the 3rd largest. Name your class Selection. Include a main method which demonstrates that your algorithm works.

Algorithm 2 Find 3rd largest element 4598 5679 3231 sort 1# 9457 2822 3457 6371 8743 3rd largest st 3 3231 4598 5679 6371 4598 5679 6371 8743 5679 9457 9457 9457 Skip 2822 8743 and 3457, nsert 6371 Insert S 9457 Insert 31

Explanation / Answer

Run this code, your program will run correctly.

#include <iostream>

using namespace std;

int main()
{
int n,i=0,k,j=0,c;
cout<<"Enter the number of elements in an array";
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cout<<"Enter the " << i+1<<" Element ";
cin>>a[i];
}
cout<<"Enter the position of largest element which you want to print ";
cin>>k;
for(i=0;i<k;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
c=a[j+1];
a[j+1]=a[j];
a[j]=c;
}
  
}
  
}
cout<<k<<" largest element is:"<< a[n-k];

return 0;
}