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
|
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "ArrayList.h"
#include "ListExceptions.h"
using namespace std;
static const int X[] = {10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000};
static const char chars[] = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
ArrayList<char> getRand(const char c[], int l)
{
ArrayList<char> trn = ArrayList<char>();
for(int i = 0; i < l; i++)
{
trn += c[rand()%62];
}
return trn;
}
char getMin(ArrayList<ArrayList<char> > c, int j, int l)
{
char min = c[0][0];
for(int i = 0; i < l; i++)
{
if(min > c[i][j])
min = c[i][j];
}
return min;
}
/* Merge-Sort Functions */
ArrayList<char>* merge(ArrayList<char>* f, ArrayList<char>* l, int s)
{
ArrayList<char>* n = new ArrayList<char>[s]();
for(int i = 0; i < s; i += 2)
{
if(f[i] > l[i])
{
n[i] = l[i];
n[i+1] = f[i];
}
else
{
n[i] = f[i];
n[i+1] = l[i];
}
}
return n;
}
ArrayList<char>* mgsort(ArrayList<char>* c, int f, int l)
{
int s = l - f;
int half = s/2;
int ind = 0;
ArrayList<char>* fst = new ArrayList<char>[half]();
ArrayList<char>* lst = new ArrayList<char>[s-half]();
while(ind < half)
{
fst[ind] = c[ind];
}
while(ind < s)
{
lst[ind] = c[ind];
}
delete[] c;
return merge(mgsort(fst, 0, half), mgsort(lst, half+1, s), s);
}
void msort(ArrayList<char>* c, int l)
{
int half = l/2;
int ind = 0;
ArrayList<char>* fst = new ArrayList<char>[half]();
ArrayList<char>* lst = new ArrayList<char>[l-half]();
while(ind < half)
{
fst[ind] = c[ind];
++ind;
}
while(ind < l)
{
lst[ind] = c[ind];
++ind;
}
c = merge(mgsort(fst, 0, half), mgsort(lst, half+1, l), l);
}
/* Radix-Sort Functions */
void rsort(ArrayList<ArrayList<char> > c, int p, int l)
{
char m;
ArrayList<ArrayList<ArrayList<char> > > t = ArrayList<ArrayList<ArrayList<char> > >();
int d = 0;
while(!c.isEmpty())
{
m = getMin(c, p, l);
for(int i = 1; i < l; i++)
{
if(c[i][p] == m)
{
t[d].append(c[i]);
c.remove(c[i]);
}
}
++d;
}
for(int i = 0; i < t.getSize(); i++)
{
rsort(t[i], p+1, t[i].getSize());
}
}
/* Main */
int main()
{
ArrayList<char>* mstrings[9];
clock_t mtimers[9];
clock_t rtimers[9];
cout << "Data structures established." << endl;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < X[i]; j++)
mstrings[i][j] = getRand(chars, rand()%10); //Error Fixed: Now initialized.
}
cout << "Strings for merge-sorting established." << endl;
ArrayList<ArrayList<char> > rstrings[9];
for(int i = 0; i < 9; i++)
rstrings[i] = ArrayList<ArrayList<char> >(mstrings[i]);
cout << "Data from mstrings copied to strings for radix sorting." << endl;
for(int i = 0; i < 9; i++)
{
mtimers[i] = clock();
msort(mstrings[i], X[i]);
mtimers[i] = clock() - mtimers[i];
}
cout << "Merge-sort complete." << endl;
for(int i = 0; i < 9; i++)
{
rtimers[i] = clock();
rsort(rstrings[i], 0, X[i]);
rtimers[i] = clock() - rtimers[i];
}
cout << "Radix-sort complete." << endl;
double mtimersF = (double)mtimers[0];
double rtimersF = (double)rtimers[0];
for(int i = 1; i < 9; i++)
{
mtimersF += (double)mtimers[i];
rtimersF += (double)rtimers[i];
}
cout << "Sorting times established." << endl;
cout << "Merge Sort: " << (mtimersF * 1000.0) / (9.0 * CLOCKS_PER_SEC) << endl;
cout << "Radix Sort: " << (rtimersF * 1000.0) / (9.0 * CLOCKS_PER_SEC) << endl;
return 0;
}
|