Question

In: Computer Science

#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...

#include <iostream>
#include <queue> //This contains the STL's efficient implementation of a priority queue using a heap
using namespace std;


/*
In this lab you will implement a data structure that supports
2 primary operations:
        - insert a new item
        - remove and return the smallest item
A data structure that supports these two operations is
called a "priority queue".  There are many ways to
implement a priority queue, with differernt efficiencies
for the two primary operations.  For this lab,
please do not attempt (at least at first) to implement
something fancy (like a heap).  Just use your mind to
do something simple.  Analyze the efficiency of both
insertion and removal for your implementation.
Afterwards, feel free to investigagte "min heaps"
to see a very clever and fast priority queue
implementation.  Feel free to try to implement it.

After implementuing your priority queue, use it to
implement a sorting algorithm (priorityQueueSort).
Analyze the run time of your sorting algorithm.

Next, plug-in the stand template libraries priority queue
(which is implemented with a heap) and compare the speed.

(don't forget to run in release mode)

To get lab credit, finish all of the coding below,
as well as be able to state the run times of
your priority queue operations and sorting algorithm
to the lab TAs.
*/


//What is the big-oh run time of this sorting routine with respect
//to the number of items n?
//How does this compare to bubble sort or selection sort?
void priorityQueueSort(int * numbers, int size)
{
        priorityQueue PQ;

        //Step 1:  insert each item from 'numbers' into PQ.

        //Step 2:  Extract from PQ until PQ is empty, each time placing the extracted item into the numbers array, one after another.

}

//For this part you will need to use the built in priority queue in the STL libary.
//The STL priority_queue is implemented using a "heap".  Feel free to read about this on your own
//to understand why it is so fast.
//The STL priority_queue has the following methods:
// push(x), which adds x to the priority queue (this is like your "insert" method)
// pop(), which removes the highest value item from the priority queue
// top(), which returns the highest value item in the priority queue (but does not remove it).
// Run time:  Inserting 1 item into a min heap takes O(log n) time.  Extracting the biggest takes O(log n).
// Therefore, the run time of this sorting algorithm is O(n log n), and that is why it sorts a billion items so fast.
// Which "fast" sort is better?  Blaze sort (aka quick sort), or heap sort?
void heapSort(int * numbers, int size)
{
        priority_queue<int> PQ;

        //Step 1:  insert each item from 'numbers' into PQ.

        //Step 2:  Extract from PQ until PQ is empty, each time placing the extracted item into the numbers array, one after another.

}


int main()
{
        //Part 1:  Implement a priority queue data structure
        priorityQueue pq;

        pq.insert(59);
        pq.insert(12);
        pq.insert(548);
        pq.insert(45);
        pq.insert(18);
        pq.insert(345);

        cout << "Extracting min: " << pq.extractMin() << endl; //12
        cout << "Extracting min: " << pq.extractMin() << endl; //18
        cout << "Extracting min: " << pq.extractMin() << endl; //45
        cout << "Extracting min: " << pq.extractMin() << endl; //59

        pq.insert(2);
        pq.insert(400);
        pq.insert(600);
        pq.insert(20);


        //2 20 345 400 548 600
        while (!pq.empty())
        {
                cout << "Extracting min: " << pq.extractMin() << endl;
        }


        //Part 2:  create a sorting function that uses your priority queue data structure to sort
        int numbers[] = { 53, 359, 31, 95, 345, 52, 13, 58, 2, 78 };

        priorityQueueSort(numbers, 10);

        for (int i = 0; i < 10; i++) //should be in sorted order now
                cout << numbers[i] << endl;


        //Part 3:  Implement the "heap sort" algorithm using the STL built in priority queue.
        int size = 10; //replace with 10000000 to stress test and time
        int * bignums = new int[size];
        for (int i = 0; i < size; i++)
                bignums[i] = rand();

        clock_t start, end;

        start = clock();
        heapSort(bignums, size);
        end = clock();

        cout << "Heap sort took: " << end - start << " milleseconds." << endl;

        //comment out display for stress test
        for (int i = 0; i < size; i++)
                cout << bignums[i] << endl;

        return 0;
}

c++

Solutions

Expert Solution

C++ Program to Implement Max Priority Queue using array
#include<iostream>
#define N 20
using namespace std;
int Q[N],Pr[N];
int r = -1,f = -1;
void enqueue(int data,int p)//Enqueue function to insert data and its priority in queue
{
        int i;
        if((f==0)&&(r==N-1)) //Check if Queue is full
                cout<<"Queue is full";
        else
        {
                if(f==-1)//if Queue is empty
                {
                        f = r = 0;
                        Q[r] = data;
                        Pr[r] = p;

                }
                else if(r == N-1)//if there there is some elemets in Queue
                {
                        for(i=f;i<=r;i++) { 
                                Q[i-f] = Q[i]; 
                                Pr[i-f] = Pr[i];
                                r = r-f; 
                                f = 0;
                                for(i = r;i>f;i--)
                                {
                                        if(p>Pr[i])
                                        {
                                                Q[i+1] = Q[i];
                                                Pr[i+1] = Pr[i];
                                        }
                                        else
                                                break;
                                        Q[i+1] = data;
                                        Pr[i+1] = p;
                                        r++;
                                }
                        }
                }
                else
                {
                        for(i = r;i>=f;i--)
                        {
                                if(p>Pr[i])
                                {
                                        Q[i+1] = Q[i];
                                        Pr[i+1] = Pr[i];        
                                }
                                else
                                        break;
                        }
                        Q[i+1] = data;
                        Pr[i+1] = p;
                        r++;
                }       
        }

}
int dequeue() //remove the data from front
{
        if(f == -1)
        {
        cout<<"Queue is Empty";
        }       
        else
        {
        cout<<"deleted Element ="<<Q[f]<<endl;
        cout<<"Its Priority = "<<Pr[f]<<endl;
                if(f==r)
                        f = r = -1;
                else
                        f++;
        }
}
int main()
{
        int opt,n,i,data,p;
        cout<<"Enter Your Choice:-"<<endl;
        do{
        cout<<"1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n0 for Exit"<<endl;
        cin>>opt;
                switch(opt){
                        case 1:
                                cout<<"Enter the number of data"<<endl;
                                cin>>n;
                                cout<<"Enter your data and Priority of data"<<endl;
                                i=0;
                                while(i<n){
                                        cin>>data;
                                        cin>>p;
                                        enqueue(data,p);
                                        i++;
                                }
                                break;
                        case 2:
                                print();
                                break;
                        case 3:
                                 dequeue();
                                break;
                        case 0:
                                break;
                        default:
                        cout<<"Incorrect Choice"<<endl;

                }
        }while(opt!=0);
        return 0;
}

priority queue using stl

#include <iostream>

#include <queue>

int main ()

{

    priority_queue <int> pq;

pq.push(10);

pq.push(20);

pq.pop();

}


Related Solutions

How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left; using std::right; using std::setw; using std::string; // The IntVariableTable constructor dynamically allocates the fixed size array of integer variables. IntVariableTable::IntVariableTable() { int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES]; } // The IntVariableTable destructor deallocates the integer variable array. IntVariableTable::~IntVariableTable() { delete[] int_variable_table; } // Returns the number of variables added to the integer variable table. int IntVariableTable::numVariables() const { return num_int_variables; } // Returns the index of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT