Advertisement
Limited_Ice

SortingAlgorithmns.h

Jul 14th, 2020
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  Class Definitions
  3. //////////////////////////////////////////////////////////////////////////////////////////////////////
  4. //////////////////////////////////////////////////////////////////////////////////////////////////////
  5. class AbstractSort {
  6. protected:
  7.  
  8.     int count = 0;
  9.     int length;
  10.  
  11. public:
  12.  
  13.     AbstractSort(int temp) {
  14.         length = temp;
  15.     }
  16.  
  17.     void numcomp() {
  18.         cout << "There have been " << count << " comparisons made." << endl;
  19.     }
  20.  
  21.     virtual void sort(int, int) = 0;
  22. };
  23.  
  24. //////////////////////////////////////////////////////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////////////////////////////////////////////////////
  26. class QuickSort : public AbstractSort {
  27.  
  28.     int* array = nullptr;
  29.    
  30.    
  31. public:
  32.  
  33.     QuickSort(int* temp, int templength) : AbstractSort(templength)
  34.     {
  35.         array = new int[length];
  36.         for (int i = 0; i < length; i++)
  37.             array[i] = temp[i];
  38.     }
  39.  
  40.     virtual void sort(int low, int high) {
  41.  
  42.         int workinglow = low;
  43.         int workinghigh = high;
  44.         int pivot = array[(workinglow + workinghigh) / 2];
  45.         int temp;
  46.  
  47.         while (workinglow <= workinghigh) {                 //  This loop executes until the pivot is reached
  48.             while (array[workinglow] < pivot) {             //  Moves the lower indedx towards the pivot if it is less
  49.                 workinglow++;
  50.                 count++;
  51.             }                                               //  than the pivot.
  52.  
  53.             while (array[workinghigh] > pivot) {                //  Moves the upper indedx towards the pivot if it is greater
  54.                 workinghigh--;                              //  than the pivot.
  55.                 count++;
  56.             }
  57.  
  58.             if (workinglow <= workinghigh) {                //***************************************
  59.                 temp = array[workinglow];                   //  This if statement swaps that values *
  60.                 array[workinglow] = array[workinghigh];     //  of the high and low index if the    *
  61.                 array[workinghigh] = temp;                  //  low index is higher than the low    *
  62.                 workinglow++;                               //  index.                              *
  63.                 workinghigh--;                              //***************************************
  64.                 count++;
  65.             }
  66.         }
  67.         if (workinghigh > low) {                        //***************************************
  68.             this->sort(low, workinghigh);               //  These Statements run the function   *
  69.         }                                               //  recursively until it is sorted      *
  70.         if (workinglow < high) {                        //                                      *
  71.             this->sort(workinglow, high);               //***************************************
  72.         }
  73.  
  74.     }
  75.  
  76.     void print() {
  77.         cout << "Your array after being sorted is: ";
  78.         for (int i = 0; i < length - 1; i++)
  79.             cout << array[i] << ", ";
  80.         cout << array[length - 1] << endl;
  81.         cout << endl;
  82.     }
  83.  
  84.     void tprint() {
  85.         for (int i = 0; i < length - 1; i++)
  86.             cout << array[i] << ", ";
  87.         cout << array[length - 1] << endl;
  88.     }
  89. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement