Advertisement
Bittle

Priority Queue

Sep 28th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #pragma once
  2.  
  3. template <class TYPE_>
  4. class priorityQueue{
  5.  
  6. private:
  7.     int capacity;
  8.     int index;
  9.     int numItems;
  10.     TYPE_ * arr;
  11.  
  12.     void sortTheArr(){
  13.        
  14.         for(int x = 0 ; x< capacity; x++){
  15.             int smallest = arr[x];
  16.             int index = x ;
  17.             for(int y = x; y < capacity; y++){
  18.                 if(arr[y] < smallest){
  19.                     smallest = arr[y];
  20.                     index = y;
  21.                 }
  22.                 TYPE_ temp = arr[index];
  23.                 arr[index] = smallest;
  24.  
  25.             }
  26.         }
  27.     }
  28.  
  29.     void resize(int newCap){
  30.         TYPE_ * arr2 = new TYPE_[newCap];
  31.  
  32.         for(int x  = 0; x < capacity; x++){
  33.             arr2[x] = arr[x];
  34.         }
  35.         delete[] arr;
  36.         arr =  arr2;
  37.         capacity = newCap;
  38.     }
  39.  
  40. public:
  41.    
  42.     priorityQueue(){
  43.         capacity = 10;
  44.         index = 0;
  45.         numItems = 0;
  46.         arr = new TYPE_[capacity];
  47.     }
  48.  
  49.     void insert(TYPE_ item){
  50.         numItems++;
  51.         if(numItems == capacity){
  52.             resize(capacity * 2);
  53.         }
  54.         else{
  55.             //cout<< "inserting "<< item << " into "<< index<<endl;
  56.             arr[index] = item;
  57.             index++;
  58.         }
  59.     }
  60.  
  61.     TYPE_ extractMin(){
  62.         if(! empty()){
  63.             int smallest = arr[0];
  64.             int ind = 0;
  65.                 for(int x = 0; x < index; x++){
  66.                     if(arr[x] < smallest){
  67.                         smallest = arr[x];
  68.                         ind = x;
  69.                     }
  70.                 }
  71.                 //if(ind != index-1)
  72.                     arr[ind] = arr[index - 1];
  73.                 index--;
  74.                 numItems --;
  75.             return smallest;
  76.         }
  77.         return -1;
  78.     }
  79.  
  80.     bool empty(){
  81.         if(numItems <= 0 )
  82.             return true;
  83.         else
  84.             return false;
  85.     }
  86.  
  87. };
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. =============================================================================================================================
  95.  
  96. #pragma once
  97. #include <iostream>
  98. #include "priorityQueue.h"
  99. using namespace std;
  100.  
  101. template <class THING>
  102. void priorityQueueSort(THING * items, int start, int end)
  103. {
  104.     //declare a variable of type priority queue
  105.     priorityQueue<double> p1;
  106.  
  107.     //insert the items from the 'items' array into the priority queue, one by one
  108.     for(int x = start; x < end+1; x++){
  109.         p1.insert(items[x]);
  110.         cout << "adding "<< items[x]<< " into "<< x<<endl;
  111.     }
  112.  
  113.     //extract the items from the priorityQueue one by one and write each value
  114.     // into the 'items' array in the order they come out.
  115.     int count = 0;
  116.     while(!p1.empty()){
  117.         int x = p1.extractMin();
  118.         //cout << "count at: "<< count << "  is: "<< x<<endl;
  119.         items[count] = x;
  120.         count++;
  121.     }
  122. }
  123.  
  124. int main()
  125. {
  126.     ///////////////////////////////////////////////////
  127.     //Step 1:  Implement an array based priority queue.
  128.     ////////////////////////////////////////////////////
  129.  
  130.     //A priority queue is an abract data type that supports
  131.     //two operations:  1) insert an item, 'insert(x)', and
  132.     //2) remove and return the smallest item, 'extractMin()'.
  133.     //Implement a priority queue using an array based implementation.
  134.     //Be prepared to answer:  
  135.     //What is the run time of your insert method?  
  136.     //What is the run time of your extractMin method?
  137.     //What is the run time of your priorityQueueSort function?
  138.  
  139.     priorityQueue<double> pq;
  140.  
  141.     pq.insert(57);
  142.     pq.insert(32);
  143.     pq.insert(105);
  144.     pq.insert(17);
  145.    
  146.     cout << pq.extractMin() << endl; //17
  147.     cout << pq.extractMin() << endl; //32
  148.     cout << endl;
  149.    
  150.     pq.insert(68);
  151.     pq.insert(5);
  152.     pq.insert(43);
  153.  
  154.     cout << pq.extractMin() << endl; //5
  155.     cout << pq.extractMin() << endl; //43
  156.     cout << pq.extractMin() << endl; //57
  157.     cout << endl;
  158.    
  159.     pq.insert(120);
  160.     pq.insert(500);
  161.     pq.insert(3);
  162.     pq.insert(73);
  163.     pq.insert(29);
  164.  
  165.    
  166.     //3 29 68 73 105 120 500
  167.     while (!pq.empty())
  168.     {
  169.         cout << pq.extractMin() << endl;
  170.     }
  171.     cout << endl;
  172.    
  173.     ///////////////////////////////////////////////////////////////////
  174.     //Step 2: now, write a sorting routine that uses a priority queue to sort
  175.     ///////////////////////////////////////////////////////////////////
  176.  
  177.     //fill a big array with random numbers
  178.     int size = 10;
  179.     int * numbers = new int[size];
  180.     for (int i = 0; i < size; i++)
  181.     {
  182.         //numbers[i] = rand();
  183.         numbers[i] = i *i;
  184.         cout << "original " << numbers[i] << endl;
  185.     }
  186.  
  187.     //sort the array using a priority queue to help you
  188.     priorityQueueSort(numbers, 0, size - 1);
  189.  
  190.     //now array should be in sorted order
  191.     for (int i = 0; i < size; i++)
  192.     {
  193.         cout << "sorted ";     
  194.         cout << numbers[i] << endl;
  195.     }
  196.    
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement