Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // Class Definitions
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- class AbstractSort {
- protected:
- int count = 0;
- int length;
- public:
- AbstractSort(int temp) {
- length = temp;
- }
- void numcomp() {
- cout << "There have been " << count << " comparisons made." << endl;
- }
- virtual void sort(int, int) = 0;
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- class QuickSort : public AbstractSort {
- int* array = nullptr;
- public:
- QuickSort(int* temp, int templength) : AbstractSort(templength)
- {
- array = new int[length];
- for (int i = 0; i < length; i++)
- array[i] = temp[i];
- }
- virtual void sort(int low, int high) {
- int workinglow = low;
- int workinghigh = high;
- int pivot = array[(workinglow + workinghigh) / 2];
- int temp;
- while (workinglow <= workinghigh) { // This loop executes until the pivot is reached
- while (array[workinglow] < pivot) { // Moves the lower indedx towards the pivot if it is less
- workinglow++;
- count++;
- } // than the pivot.
- while (array[workinghigh] > pivot) { // Moves the upper indedx towards the pivot if it is greater
- workinghigh--; // than the pivot.
- count++;
- }
- if (workinglow <= workinghigh) { //***************************************
- temp = array[workinglow]; // This if statement swaps that values *
- array[workinglow] = array[workinghigh]; // of the high and low index if the *
- array[workinghigh] = temp; // low index is higher than the low *
- workinglow++; // index. *
- workinghigh--; //***************************************
- count++;
- }
- }
- if (workinghigh > low) { //***************************************
- this->sort(low, workinghigh); // These Statements run the function *
- } // recursively until it is sorted *
- if (workinglow < high) { // *
- this->sort(workinglow, high); //***************************************
- }
- }
- void print() {
- cout << "Your array after being sorted is: ";
- for (int i = 0; i < length - 1; i++)
- cout << array[i] << ", ";
- cout << array[length - 1] << endl;
- cout << endl;
- }
- void tprint() {
- for (int i = 0; i < length - 1; i++)
- cout << array[i] << ", ";
- cout << array[length - 1] << endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement