Advertisement
rinab333

CSCI 340 assign 7

Jul 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. //Terina BUrr
  7. //Assignment 7
  8. //Due 11/06/15
  9. //Section 1
  10.  
  11. using namespace std;
  12. int NO_ITEMS = 10;
  13. int ITEM_W=4;
  14.  
  15. void build_heap ( vector < int >& v, int heap_size, bool (*compar)(int, int) );
  16. void heapify( vector < int >& v, int heap_size, int r, bool (*compar)(int, int) );
  17. bool less_than ( int e1, int e2 );
  18. bool greater_than ( int e1, int e2 );
  19. void heap_sort ( vector < int >& v, int heap_size, bool (*compar)(int, int) );
  20. int extract_heap ( vector < int >& v, int& heap_size, bool (*compar)(int, int) );
  21. void print_vector ( vector < int >& v, int pos, int size );
  22. /***************************************************************
  23. Function: build heap
  24.  
  25. Use:      It builds the heap by invoking heapify
  26.  
  27. Arguments:  1. &v: reference to a vector that you make the heap out of          
  28.         2. heap_size:  the size of the heap
  29.         3. compar: pointer to a function accepting two int arguments and returning an integer
  30.  
  31. Returns:   nothing
  32. ***************************************************************/
  33.  
  34. void build_heap ( vector < int >& v, int heap_size,bool (*compar)(int x, int y) )
  35. {
  36.     for(int i = heap_size/2; i>= 0; i--)
  37.     {
  38.         heapify(v, heap_size, i , compar);
  39.     }
  40.  
  41. }
  42. /***************************************************************
  43. Function: heapify
  44.  
  45. Use:      specifies the size of the whole heap contained by the vector uses function compar to compare two functions
  46.  
  47. Arguments:  1. &v: reference to a vector
  48.         2. heap_size:  the size of the heap
  49.         3. compar: pointer to a function accepting two int arguments and returning an integer
  50.         4. r where the function heapifies what position
  51. Returns:   nothing
  52. ***************************************************************/
  53.  
  54. void heapify( vector < int >& v, int heap_size, int r, bool (*compar)(int x, int y) )
  55. {
  56.     int min = r;
  57.     if( 2*r<=heap_size && compar(v[2*r],v[r]) )
  58.         min = 2*r;
  59.     if(2*r+1<= heap_size&&compar(v[2*r+1],v[min]))
  60.         min = 2* r +1;
  61.     if (min!=r)
  62.     {
  63.         int temp = v[r];
  64.         v[r] = v[min];
  65.         v[min] = temp;
  66.         heapify(v,heap_size, min,compar );
  67.     }
  68. }
  69. /****************
  70. Function: less than
  71.  
  72. Use:      sees if e1 is less than e2
  73.  
  74. Arguments:  1. E1: int that e2 is compared to          
  75.         2. e2: int that e1 is compared to
  76.        
  77. Returns:   true or false if e1 is less than e2
  78. ***************************************************************/
  79.  
  80. bool less_than ( int e1, int e2 )
  81. {
  82.     if(e1<e2)
  83.         return true;
  84.     else
  85.         return false;
  86. }
  87. /***************************************************************
  88. Function: greater than
  89.  
  90. Use:      sees if e1 is greater than e2
  91.  
  92. Arguments:  1. E1: int that e2 is compared to          
  93.         2. e2: int that e1 is compared to
  94.        
  95. Returns:   true or false if e1 is greater than e2
  96. ***************************************************************/
  97.  
  98. bool greater_than ( int e1, int e2 ){
  99.     if(e1>e2)
  100.         return true;
  101.     else
  102.         return false;
  103. }
  104. /***************************************************************
  105. Function: heap_sort
  106.  
  107. Use:      it sorts the heap
  108.  
  109. Arguments:  1. &v: reference to a vector
  110.         2. heap_size:  the size of the heap
  111.         3. compar: pointer to a function accepting two int arguments and returning an integer
  112.  
  113. Returns:   nothing
  114. ***************************************************************/
  115.  
  116. void heap_sort ( vector < int >& v, int heap_size, bool (*compar)(int, int) )
  117. {
  118. //                extract_heap(v,heap_size,compar);
  119.     for(unsigned int i = 0 ; i < v.size() ; ++i) //going through the vector
  120.     {
  121.     int temp = i+1;
  122.     while(temp != 1 && compar(v[temp/2-1],v[temp-1]))
  123.     {
  124.         std::swap(v[temp-1],v[temp/2-1]);//swap values if
  125.         temp /= 2;//make temp = temp/2
  126.     }  
  127.     }
  128.     //sorting the heap
  129.     for(int i = v.size() ; i > 1 ; --i)
  130.     {
  131.     std::swap(v[0],v[i-1]);
  132.     if(v[i] == 1000000) // -10000)
  133.     v[i] =   extract_heap(v,heap_size,compar);//need to extract the heap but wasnt sure where to put it
  134.    
  135.  
  136.     int temp = 1;
  137.     while( (temp*2   < i && compar(v[temp-1],v[temp*2-1])  ) ||
  138.            (temp*2+1 < i && compar(v[temp-1],v[temp*2]))  )
  139.     {
  140.         int temp2;
  141.         if(temp*2+1 < i && compar(v[temp*2-1],v[temp*2]))
  142.         {
  143.         temp2 = temp*2+1;
  144.         }  
  145.          else
  146.         {
  147.         temp2 = temp*2;
  148.         }
  149.         std::swap(v[temp-1],v[temp2-1]);
  150.  
  151.      temp = temp2;
  152.     }
  153.     }
  154.  
  155. }
  156. /***************************************************************
  157. Function: extract heap
  158.  
  159. Use:      it extracts the root of the tree and replaces it with the last value and returns the last value
  160.  
  161. Arguments:  1. &v: reference to a vector        
  162.         2. heap_size:  the size of the heap
  163.         3. compar: pointer to a function accepting two int arguments and returning an integer
  164.  
  165. Returns:   the old root
  166. ***************************************************************/
  167.  
  168. int extract_heap ( vector < int >& v, int& heap_size, bool(*compar)(int, int) )
  169. {
  170.     int root = v[1];
  171.     int temp = root;
  172.     int last = v[heap_size-1];
  173.     root = last;
  174.     v[1] = last;
  175.     heapify(v,heap_size,v[0],compar);
  176.  //is suppose to extract the root and put it where the last element is
  177.     return temp;
  178. }
  179. /***************************************************************
  180. Function: print vector
  181.  
  182. Use:      prints vector
  183.  
  184. Arguments:  1. &v: reference to a vector
  185.         2. size:  the size of the vector
  186.         3. pos: position where you start
  187.  
  188. Returns:   nothing
  189. ***************************************************************/
  190.  
  191.  
  192. void print_vector ( vector < int >& v, int pos, int size )
  193. {
  194.     vector<int>::iterator iter;
  195.     int count =0;
  196.     iter = v.begin()+pos;
  197.     while(iter !=v.end())
  198.     {
  199.         count++;
  200.         cout << setw(ITEM_W) << *iter;
  201. //prints the numbers at a width that is four spaces apart
  202.             if(count % NO_ITEMS == 0)
  203.             {
  204.                 cout <<endl;
  205.             }
  206. //if there is more than 10 numbers on a line then they start a new line
  207.         iter++;
  208.     }
  209. cout<<endl;
  210.  
  211. }
  212.  
  213.  
  214.  
  215.  
  216. int main(int argc, char** argv) {
  217.     // ------- creating input vector --------------
  218.     vector<int> v;
  219.     v.push_back(-1000000);    // first element is fake    
  220.     int heap_size = 24;
  221.     for (int i=1; i<=heap_size; i++)
  222.         v.push_back( i );
  223.     random_shuffle( v.begin()+1, v.begin()+heap_size+1 );
  224.     cout << "\nCurrent input numbers: " << endl;
  225.     print_vector( v, 1, heap_size );
  226.  
  227.     // ------- testing min heap ------------------
  228.     cout << "\nBuilding a min heap..." << endl;
  229.     build_heap(v, heap_size, less_than);
  230.     cout << "Min heap: " << endl;
  231.     print_vector( v, 1, heap_size );
  232.     heap_sort( v, heap_size, less_than);
  233.     cout << "Heap sort result (in ascending order): " << endl;
  234.     print_vector( v, 1, heap_size );
  235.    
  236.     // ------- testing max heap ------------------
  237.     cout << "\nBuilding a max heap..." << endl;
  238.     build_heap(v, heap_size, greater_than);
  239.     cout << "Max heap: " << endl;
  240.     print_vector( v, 1, heap_size );
  241.     heap_sort( v, heap_size, greater_than);
  242.     cout << "Heap sort result (in descending order): " << endl;
  243.     print_vector( v, 1, heap_size );
  244.                        
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement