Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. ****main****
  2. #include "SelectionSort.h"
  3. #include "MergeSort.h"
  4. #include "HeapSort.h"
  5. #include "Quicksort.h"
  6. #include <fstream>
  7. #include <ctime>
  8.  
  9. //#include <vector>
  10.  
  11. int main()
  12. {
  13.     unsigned int size = 10000000;
  14.     ifstream inFile("numbers.txt");
  15.     inFile >> size;
  16.     unsigned int * nums = new unsigned int[size];
  17.     for (int i = 0; i < size; i++)
  18.     {
  19.         inFile >> nums[i];
  20.         //cout << "number " << i << ": " << nums[i] << endl;
  21.     }
  22.    
  23.     inFile.close();
  24.    
  25.     Sort obj(size);
  26.     Merge obj2(size);
  27.     Heap obj3(size);
  28.     Quick obj4(size);
  29.  
  30.     clock_t startTime, endTime;
  31.  
  32.     startTime = clock();
  33.     //obj4.quickSort(nums, 0, size-1);
  34.     obj3.heapSort(nums, size);
  35.     //obj2.mergeSort(nums, 0, size - 1);
  36.     //obj.selectionSort(nums, size);
  37.     endTime = clock();
  38.  
  39.     ofstream outFile("numbersNew.txt");
  40.     for(int i = 0; i < size; i++)
  41.     {
  42.         outFile << nums[i] << endl;
  43.     }
  44.     outFile.close();
  45.  
  46.     cout << "Sort took: " << endTime - startTime << " milliseconds" << endl;
  47. //
  48. //    //for (int i = 0; i < size; i++)
  49. //    //    cout << nums[i] << endl;
  50.    
  51.     return 0;
  52. }
  53.  
  54. ****selectionSort.h****
  55. //I got this algorithm from geeksforgeeks.org
  56. #include <iostream>
  57. using namespace std;
  58.  
  59. class Sort
  60. {
  61. private:
  62.     unsigned int * list;
  63.     unsigned int numItems;
  64. //
  65.    
  66. public:
  67.     Sort()
  68.     {
  69.         numItems = 0;
  70.         list = new unsigned int[numItems];
  71.     }
  72.     Sort(int cap)
  73.     {
  74.         numItems = cap;
  75.         list = new unsigned int[numItems];
  76.     }
  77.     void swap(unsigned int *x, unsigned int *y)
  78.     {
  79.         int temp = *x;
  80.         *x = *y;
  81.         *y = temp;
  82.     }
  83.    
  84.     void selectionSort(unsigned int * elements, int size)
  85.     {
  86.         int minIndex;
  87.         for (int i = 0; i < size-1; i++) //traverse through the unsorted array to hold the current min
  88.         {
  89.            
  90.             minIndex = i;
  91.             //A second iterator traverses the array looking for an elements smaller than the current min, if it does so then min will be set to the new current min.
  92.             for(int j = i+1; j < size; j++)
  93.             {
  94.                
  95.                 if(elements[j] < elements[minIndex])
  96.                 {
  97.                     minIndex = j;
  98.                 }
  99.             }
  100.             //swap the current min with the new min
  101.             swap(&elements[minIndex], &elements[i]);
  102.         }
  103.        
  104.     }
  105.    
  106.     /* Function to print an array */
  107.     void printArray(unsigned int * elements, int size)
  108.     {
  109.         for(int i = 0; i < size; i++)
  110.         {
  111.             cout << elements[i] << endl;
  112.         }
  113.     }
  114.    
  115.  
  116. };
  117.  
  118. ****MergeSort****
  119. //I got this algorithm from geeksforgeeks.org
  120. #include <iostream>
  121. using namespace std;
  122. class Heap
  123. {
  124. private:
  125.     unsigned int * list;
  126.     unsigned int numItems;
  127.    
  128. public:
  129.     Heap()
  130.     {
  131.         numItems = 0;
  132.         list = new unsigned int[numItems];
  133.     }
  134.     Heap(int cap)
  135.     {
  136.         numItems = cap;
  137.         list = new unsigned int[numItems];
  138.     }
  139.     void heapify(unsigned int * elements, int n, int i)
  140.     {
  141.         int largest = i;  // Initialize largest as root
  142.         int l = 2*i + 1;  // left = 2*i + 1
  143.         int r = 2*i + 2;  // right = 2*i + 2
  144.        
  145.         // If left child is larger than root
  146.         if (l < n && elements[l] > elements[largest])
  147.             largest = l;
  148.        
  149.         // If right child is larger than largest so far
  150.         if (r < n && elements[r] > elements[largest])
  151.             largest = r;
  152.        
  153.         // If largest is not root
  154.         if (largest != i)
  155.         {
  156.             swap(elements[i], elements[largest]);
  157.            
  158.             // Recursively heapify the affected sub-tree
  159.             heapify(elements, n, largest);
  160.         }
  161.     }
  162.    
  163.     void heapSort(unsigned int * elements, int n)
  164.     {
  165.         // Build heap (rearrange array)
  166.         for (int i = n / 2 - 1; i >= 0; i--)
  167.             heapify(elements, n, i);
  168.        
  169.         // One by one extract an element from heap
  170.         for (int i=n-1; i>=0; i--)
  171.         {
  172.             // Move current root to end
  173.             swap(elements[0], elements[i]);
  174.            
  175.             // call max heapify on the reduced heap
  176.             heapify(elements, i, 0);
  177.         }
  178.     }
  179.    
  180.     void printArray(unsigned int * elements, int size)
  181.     {
  182.         for (int i = 0; i < size; i++)
  183.         {
  184.             cout << elements[i] << endl;
  185.         }
  186.     }
  187.    
  188.    
  189. };
  190.  
  191. ****QuickSort.h****
  192. #include <iostream>
  193. using namespace std;
  194.  
  195. class Quick
  196. {
  197. private:
  198.     unsigned int numItems;
  199.     unsigned int * list;
  200.    
  201.     void swap(unsigned int * x, unsigned int * y)
  202.     {
  203.         int temp = *x;
  204.         *x = *y;
  205.         *y = temp;
  206.     }
  207. public:
  208.     Quick()
  209.     {
  210.         numItems = 0;
  211.         list = new unsigned int[numItems];
  212.     }
  213.     Quick(int cap)
  214.     {
  215.         numItems = cap;
  216.         list = new unsigned int[numItems];
  217.     }
  218.     /* This function takes last element as pivot, places
  219.      the pivot element at its correct position in sorted
  220.      array, and places all smaller (smaller than pivot)
  221.      to left of pivot and all greater elements to right
  222.      of pivot */
  223.     int partition (unsigned int * elements, int low, int high)
  224.     {
  225.         int pivot = elements[high];    // pivot
  226.         int i = (low - 1);  // Index of smaller element
  227.        
  228.         for (int j = low; j <= high- 1; j++)
  229.         {
  230.             // If current element is smaller than or
  231.             // equal to pivot
  232.             if (elements[j] <= pivot)
  233.             {
  234.                 i++;    // increment index of smaller element
  235.                 swap(&elements[i], &elements[j]);
  236.             }
  237.         }
  238.         swap(&elements[i + 1], &elements[high]);
  239.         return (i + 1);
  240.     }
  241.    
  242.     /* The main function that implements QuickSort
  243.      arr[] --> Array to be sorted,
  244.      low  --> Starting index,
  245.      high  --> Ending index */
  246.     void quickSort(unsigned int * elements, int low, int high)
  247.     {
  248.         if (low < high)
  249.         {
  250.             /* pi is partitioning index, arr[p] is now
  251.              at right place */
  252.             int pi = partition(elements, low, high);
  253.            
  254.             // Separately sort elements before
  255.             // partition and after partition
  256.             quickSort(elements, low, pi - 1);
  257.             quickSort(elements, pi + 1, high);
  258.         }
  259.     }
  260.    
  261.     void printArray(unsigned int * elements, int size)
  262.     {
  263.         for (int i = 0; i < size; i++)
  264.         {
  265.             cout << elements[i] << endl;
  266.         }
  267.     }
  268. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement