#include #include //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) */ //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 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; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#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) */ //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; }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education