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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
const int NUM_SIZES = 5;
typedef int DataType;
int indexOfLargest(const DataType[],
const int);
void swap(DataType&,
DataType&);
void partition(DataType theArray[],int first, int last, int& pivotIndex);
void merge(DataType theAray[],int first,int mid, int last);
void selectionSort(DataType[],
const int);
//BubbleSort
void bubbleSort(DataType[],const int);
//InsertionSort
void insertionSort(DataType[],const int);
//MergeSort
void mergeSort(DataType[],int,int);
//QuickSort
void quickSort(DataType[],int,int);
//ShellSort
void shellSort(DataType[],int);
void printResults(int[][NUM_SIZES]);
enum SortType {SELECTION,
BUBBLE,
INSERTION,
MERGE,
QUICK,
SHELL};
int main() {
cout << "clockTicksPerSec = "
<< CLOCKS_PER_SEC
<< endl
<< endl;
// Seed the random number generator with the current time
srand( (unsigned int)time(0) );
const int SIZES[NUM_SIZES] = {10000, // 10K
20000, // 20K
40000, // 40K
80000, // 80K
160000}; // 160K
clock_t startTime, endTime;
int results[SHELL + 1][NUM_SIZES] = {0};
for (int num = 0; num < NUM_SIZES; ++num) {
// Main data to use in each sort routine
DataType* dataArray = new int[SIZES[num]];
// This array will be used to set 'dataArray' back to unsorted
// state after each sort routine has finished with it
DataType* backupArray = new int[SIZES[num]];
// Generate array of random data (and its backup)
for (int i = 0; i < SIZES[num]; ++i) {
DataType randomNumber = rand() % SIZES[num] + 1;
dataArray[i] = randomNumber;
backupArray[i] = randomNumber;
}
////////SELECTION///////////////////////////////////////////
// Get the time just before 'selectionSort' starts
startTime = clock();
// Sort the array
selectionSort(dataArray, SIZES[num]);
// Get the time just after 'selectionSort' finishes
endTime = clock();
// Store the elapsed time required to selection sort an array
// with SIZES[num] items in it.
results[SELECTION][num] = endTime - startTime;
// Restore the original (unsorted) data in 'dataArray'
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
/////////////////////////////////////////////////////////////
////////BUBBLESORT//////////////////////////////////////////
startTime = clock();
bubbleSort(dataArray,SIZES[num]);
endTime = clock();
results[BUBBLE][num] = endTime - startTime;
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
////////////////////////////////////////////////////////////
/////////INSERTION//////////////////////////////////////////
startTime = clock();
insertionSort(dataArray,SIZES[num]);
endTime = clock();
results[INSERTION][num] = endTime - startTime;
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
////////////////////////////////////////////////////////////
////////MERGE///////////////////////////////////////////////
startTime = clock();
mergeSort(dataArray,0,4);
endTime = clock();
results[MERGE][num] = endTime - startTime;
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
////////////////////////////////////////////////////////////
////////QUICK///////////////////////////////////////////////
startTime = clock();
int first = dataArray[0];
int last = dataArray[(SIZES[num] - 1)];
quickSort(dataArray,first,last);
endTime = clock();
results[QUICK][num] = endTime - startTime;
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
////////////////////////////////////////////////////////////
////////SHELL///////////////////////////////////////////////
startTime = clock();
shellSort(dataArray,SIZES[num]);
endTime = clock();
results[SHELL][num] = endTime - startTime;
for (int i = 0; i < SIZES[num]; ++i) {
dataArray[i] = backupArray[i];
}
////////////////////////////////////////////////////////////
// Release dynamic memory used
delete [] dataArray;
delete [] backupArray;
}
// Output the results of all runs
printResults(results);
return 0;
}
int indexOfLargest(const DataType theArray[],
const int size) {
int indexSoFar = 0;
for (int currentIndex = 1; currentIndex < size; ++currentIndex) {
if (theArray[currentIndex] > theArray[indexSoFar]) {
indexSoFar = currentIndex;
}
}
return indexSoFar;
}
void swap(DataType& x,
DataType& y) {
DataType temp = x;
x = y;
y = temp;
}
void selectionSort(DataType theArray[],
const int n) {
for (int last = n - 1; last >= 1; --last) {
int largest = indexOfLargest(theArray, last + 1);
swap(theArray[largest], theArray[last]);
}
}
void bubbleSort(DataType theArray[], const int n)
{
bool sorted = false;
int pass;
for (pass = 1; (pass < n) && !sorted; ++pass)
{
sorted = true;
for (int index = 0; index < n-pass; ++index)
{
int nextIndex = index + 1;
if (theArray[index] > theArray[nextIndex])
{
swap(theArray[index], theArray[nextIndex]);
sorted = false;
}
}
}
}
void insertionSort(DataType theArray[], const int n)
{
for (int unsorted = 1; unsorted < n; ++unsorted)
{
DataType nextItem = theArray[unsorted];
int loc = unsorted;
for (;(loc > 0) && (theArray[loc - 1] > nextItem);--loc)
theArray[loc] = theArray[loc - 1];
theArray[loc] = nextItem;
}
}
void merge(DataType theArray[],int first,int mid, int last)
{
DataType tempArray[5];
int first1 = first;
int last1 = mid;
int first2 = mid + 1;
int last2 = last;
int index = first1;
for (;(first1 <= last1) && (first2 <= last2); ++index)
{
if (theArray[first1] < theArray[first2])
{ tempArray[index] = theArray[first1];
++first1;
}
else
{ tempArray[index] = theArray[first2];
++first2;
}
}
for (; first1 <= last1; ++first1, ++index)
tempArray[index] = theArray[first1];
for (; first2 <= last2; ++first2, ++index)
tempArray[index] = theArray[first2];
for (index = first;index <= last; ++index)
theArray[index] = tempArray[index];
}
void mergeSort(DataType theArray[],int first,int last)
{
if ( first < last)
{
int mid = 2;
mergeSort(theArray,first,mid);
mergeSort(theArray,3,last);
merge(theArray,first,mid,last);
}
}
|