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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#include <iostream>
#include <functional>
#include <ctime>
#include <fstream>
using namespace std;
void RandomArray(int sort[], int size);
int NumberOfElements(int size);
void MergeSort(int sort[], int start, int end);
void Merge(int sort[], int start, int mid, int end);
void quickSort(int sort[], int start, int end);
void ArrayCopy(int sort[], int sortNew[], int size);
void ArrayReplace(int sort[], int sortNew[], int size);
void ReverseArray(int sort[], int size);
void printArray(int sort[], int size);
int main()
{
int size, method;
clock_t sec;
size = NumberOfElements(size); //Get size of Array
int sort[size], sortNew[size];
int start = 0, end = size - 1;
RandomArray(sort, size); //Generate random array
ArrayCopy(sort, sortNew, size); //Copy to another array
cout << "Which sorting method would you like to use? ";
while (method != 6)
{
cout << endl;
cout << "Press 1 for Merge Sort: " << endl;
cout << "Press 2 for Quick Sort: " << endl;
cin >> method;
switch (method)
{
case 1:
cout << "Sort random array: \n";
sec = clock();
MergeSort(sort, start, end); //Sorting random array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds \n" << endl;
cout << "Sorted input data set: \n";
sec = clock();
MergeSort(sort, start, end); //Sorting input data array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds \n" << endl;
ReverseArray(sort, size);
cout << "Sorting reverse array: \n";
sec = clock();
MergeSort(sort, start, end); //Sort reversed array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds \n" << endl;
printArray(sort, size);
ArrayReplace(sort, sortNew, size);
break;
case 2:
cout << "Sort random array: \n";
sec = clock();
quickSort(sort, start, end); //Sorting random array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds \n" << endl;
cout << "Sorted input data set: \n";
sec = clock();
quickSort(sort, start, end); //Sorting input data array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds" << endl;
ReverseArray(sort, size);
cout << "Sorting reverse array: \n";
sec = clock();
quickSort(sort, start, end); //Sort reversed array
cout << "CPU clock time: " << (double)(clock() - sec) / CLOCKS_PER_SEC << " Seconds" << endl;
printArray(sort, size);
ArrayReplace(sort, sortNew, size);
break;
case 3:
break;
default: cout << "Wrong number entered! " << endl;
}
}
return 0;
}
void RandomArray(int sort[], int size)
{
for (int i = 0; i<size; i++)
{
bool check; //variable to check or number is already used
int n; //variable to store the number in
do
{
n = rand() % size;
//check or number is already used:
check = true;
for (int j = 0; j<i; j++)
if (n == sort[j]) //if number is already used
{
check = false; //set check to false
break; //no need to check the other elements of value[]
}
} while (!check); //loop until new, unique number is found
sort[i] = n; //store the generated number in the array
}
}
void ArrayCopy(int sort[], int sortNew[], int size)
{
for (int i = 0; i<size; i++)
{
sortNew[i] = sort[i];
}
}
void ArrayReplace(int sort[], int sortNew[], int size)
{
for (int i = 0; i<size; i++)
sort[i] = sortNew[i];
}
void ReverseArray(int sort[], int size)
{
int start = 0;
int end = size - 1;
while (start < end)
{
int temp = sort[start];
sort[start] = sort[end];
sort[end] = temp;
start++;
end--;
}
}
int NumberOfElements(int size)
{
int elements;
cout << " Press 1 for 100 elements" << endl;
cout << " Press 2 for 200 elements" << endl;
cout << " Press 3 for 300 elements" << endl;
cout << " Press 4 for 400 elements" << endl;
cout << " Press 5 for 500 elements" << endl;
cout << " Press 6 for 1000 elements" << endl;
cout << " Press 7 for 2000 elements" << endl;
cout << " Press 8 for 4000 elements" << endl;
cout << " Press 9 for 10000 elements" << endl;
cin >> elements;
if (elements == 1)
return 100;
else if (elements == 2)
return 200;
else if (elements == 3)
return 300;
else if (elements == 4)
return 400;
else if (elements == 5)
return 500;
else if (elements == 6)
return 1000;
else if (elements == 7)
return 2000;
else if (elements == 8)
return 4000;
else if (elements == 9)
return 10000;
else
cout << "Wrong selection!";
}
void MergeSort(int sort[], int start, int end)
{
if (start<end)
{
int mid = (start + end) / 2;
MergeSort(sort, start, mid);
MergeSort(sort, mid + 1, end);
Merge(sort, start, mid, end);
}
}
void Merge(int sort[], int start, int mid, int end)
{
int *temp = new int[end - start + 1]; //temporary merger array
int i = start, j = mid + 1; //i is for left-hand,j is for right-hand
int k = 0; //k is for the temporary array
while (i <= mid && j <= end)
{
if (sort[i] <= sort[j])
temp[k++] = sort[i++];
else
temp[k++] = sort[j++];
}
//rest elements of left-half
while (i <= mid)
temp[k++] = sort[i++];
//rest elements of right-half
while (j <= end)
temp[k++] = sort[j++];
//copy the merged temporary array to the original array
for (k = 0, i = start; i <= end; ++i, ++k)
sort[i] = temp[k];
delete[]temp;
}
void quickSort(int sort[], int start, int end)
{
int i = start, j = end;
int tmp;
int pivot = sort[(start + end) / 2];
/* partition */
while (i <= j)
{
while (sort[i] < pivot)
i++;
while (sort[j] > pivot)
j--;
if (i <= j)
{
tmp = sort[i];
sort[i] = sort[j];
sort[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (start < j)
quickSort(sort, start, j);
if (i < end)
quickSort(sort, i, end);
}
void printArray(int sort[], int size)
{
cout << "Sorted array: ";
for (int i = 0; i < size; i++)
cout << sort[i] << ", ";
cout << endl;
}
| |