1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
//my .cpp file
#include "SortData.h"
#include <cstdlib>
#include <iostream>
using namespace std;
//--------------------------------------------------------------------------------------------------------------------
//Definition of SortData----------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------
//uses the new operator to allocate theData array, with the size specified in the constructor parameter list.
//sets maxSize (data from the protected section of the base class, of type int) to the specified size if a
//valid size (e.g. > 0) or to 100 by default.
SortData::SortData(int max)
{
if (max > 1) //If the input is valid or not specified and defaults to 100...
{
theData = new long[max];
maxSize = max;
}
else //If the input is invalid...
{
theData = new long[100];
maxSize = 100;
}
}
//destructor
SortData::~SortData()
{
delete [] theData;
}
//returns the size of theData array, which is an array of values of type long.
int SortData::size() const
{
return maxSize;
}
//fills the contents of theData array with data values obtained from the built-in random number generator.
//if no seed is specified when calling randomize(), the default of 1 is passed as the seed.
void SortData::randomize(int seed)
{
srand(seed);
for(int i=0; i<maxSize; ++i)
theData[i] = rand() * rand();
}
//prints out the first “num” values of theData, and the last “num” values of theData. If not specified, it will
//print out 10 data values by default (the first and last 5, if implemented that way).
/*void SortData::printSome(const int num) const
{
for (int i = 0; i < num/2; i++)
cout << theData[i] << endl;
cout << endl;
for (int j = maxSize-(num/2); j < maxSize; j++)
cout << theData[j] << endl;
}*/
void SortData::printSome(const int num) const
{
for (int i = 0; i < num; i++)
cout << theData[i] << endl;
cout << endl;
}
//--------------------------------------------------------------------------------------------------------------------
//Definition of QuickSort---------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------
//uses the new operator to allocate theData array, with the size specified in the constructor parameter list.
//the default size of theData array will be 100 if not specified. theData is a pointer to an array of type
//long and is inherited from the protected section of the base class.
//sets maxSize (data from the protected section of the base class, of type int) to the specified size if a
//valid size (e.g. > 0) or to 100 by default.
QuickSort::QuickSort (int max)
{
if (max > 1) //If the input is valid or not specified and defaults to 100...
{
theData = new long[max];
maxSize = max;
}
else //If the input is invalid...
{
theData = new long[100];
maxSize = 100;
}
}
//destructor
QuickSort::~QuickSort ()
{
delete [] theData;
}
//sorts theData array, and returns a count (numops) of the number of significant operations that were required.
//The count is kept track of within the private piece of data called numops, of type _int64.
_int64 QuickSort::sort()
{
numops = 0;
quicksort(theData, maxSize);
return numops;
}
//'helper' method to accomplish the actual sorting when called by the sort() function.
//quicksort takes an array of data of type long and the size of that array as parameters and then sorts
//the array. Once it is sorted, the public method will take over again and return numops.
void QuickSort::quicksort(long theArray[], int n)
{
int left = 0;
int right = n;
int temp;
int pivot = (left+right)/2;
while (left <= right) //Partition the array.
{
while (theArray[left] < theArray[pivot]) //Increment until we need to swap left.
left++;
while (theArray[right] > theArray[pivot]) //Decrement until we need to swap right.
right--;
//Since we now need to swap...
if (left <= right) //Avoids special case of done partitioning...
{
temp = theArray[left];
theArray[left] = theArray[right];
theArray[right] = temp;
left++;
right--;
}
} //End partitioning of the array.
if (pivot > 1) //If left partition needs to be sorted...
quicksort(theArray, pivot);
if (pivot < n-2) //If right partition needs to be sorted...
quicksort(theArray, (n-pivot-1) );
}
|