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 <math.h> #include <queue> #include <time.h> #include <fstream>
using namespace std;
int *LSD(int *a, int f, int l) { int n = l - f + 1; int m = log(n) / log(10); queue<int> q[10]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ int t = a[f + j] % (int)pow(10, i + 1); t = t / (int)pow(10, i); q[t].push(a[f + j]); } int k = 0; for(int j = 0; j < 10; j++){ while(!q[j].empty()){ a[f + k] = q[j].front(); q[j].pop(); k++; } } } return a; }
int *MSD(int *a, int f, int l) { int n = l - f + 1; int m = log(n) / log(10); queue<int> q[10]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ int t = a[f + j] / (int)pow(10, i); t = t % 10; q[t].push(a[f + j]); } int k = 0; for(int j = 0; j < 10; j++){ while(!q[j].empty()){ a[f + k] = q[j].front(); q[j].pop(); k++; } } } return a; }
int *QS(int *a, int f, int l) { if(f >= l) return a; int i = f, j = l; int x = a[f]; while(i < j){ while(i < j && a[j] >= x) j--; if(i < j) a[i++] = a[j]; while(i < j && a[i] <= x) i++; if(i < j) a[j--] = a[i]; } a[i] = x; QS(a, f, i - 1); QS(a, i + 1, l); return a; }
int *MS(int *a, int f, int l) { if(f >= l) return a; int m = (f + l) / 2; MS(a, f, m); MS(a, m + 1, l); int *b = new int[l - f + 1]; int i = f, j = m + 1, k = 0; while(i <= m && j <= l){ if(a[i] < a[j]) b[k++] = a[i++]; else b[k++] = a[j++]; } while(i <= m) b[k++] = a[i++]; while(j <= l) b[k++] = a[j++]; for(int i = 0; i < k; i++) a[f + i] = b[i]; return a; }
int *SS(int *a, int f, int l) { int n = l - f + 1; int h = 1; h = n / 2; while(h >= 1){ for(int i = f + h; i <= l; i++){ int t = a[i]; int j = i - h; while(j >= f && a[j] > t){ a[j + h] = a[j]; j -= h; } a[j + h] = t; } h /= 2; } return a; }
int *IS(int *a, int f, int l){ for(int i = f + 1; i <= l; i++){ int t = a[i]; int j = i - 1; while(j >= f && a[j] > t){ a[j + 1] = a[j]; j--; } a[j + 1] = t; } return a; }
int *BS(int *a, int f, int l){ for(int i = f; i < l; i++){ for(int j = f; j < l - i; j++){ if(a[j] > a[j + 1]){ int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } return a; }
int main(){ int n = 10000; int a[n]; for(int i = 0; i < n; i++){ a[i] = rand() % 10000; } ofstream fout("data.txt"); fout << "排序前:" << endl; for(int i = 0; i < n; i++){ fout << a[i] << " "; } fout << endl; fout.close(); clock_t time1 = clock(); int *b = LSD(a,0,n-1); clock_t time2 = clock(); fout.open("data.txt", ios::app); fout << "LSD基数排序后:" << endl; for(int i = 0; i < n; i++){ fout << b[i] << " "; } fout << endl; fout.close();
clock_t time3 = clock(); int *c = MSD(a,0,n-1); clock_t time4 = clock(); fout.open("data.txt", ios::app); fout << "MSD基数排序后:" << endl; for(int i = 0; i < n; i++){ fout << c[i] << " "; } fout << endl; fout.close();
clock_t time5 = clock(); int *d = QS(a,0,n-1); clock_t time6 = clock(); fout.open("data.txt", ios::app); fout << "QS快速排序后:" << endl; for(int i = 0; i < n; i++){ fout << d[i] << " "; } fout << endl; fout.close();
clock_t time7 = clock(); int *e = MS(a,0,n-1); clock_t time8 = clock(); fout.open("data.txt", ios::app); fout << "MS归并排序后:" << endl; for(int i = 0; i < n; i++){ fout << e[i] << " "; } fout << endl; fout.close();
clock_t time9 = clock(); int *f = SS(a,0,n-1); clock_t time10 = clock(); fout.open("data.txt", ios::app); fout << "SS希尔排序后:" << endl; for(int i = 0; i < n; i++){ fout << f[i] << " "; } fout << endl; fout.close();
clock_t time11 = clock(); int *g = IS(a,0,n-1); clock_t time12 = clock(); fout.open("data.txt", ios::app); fout << "IS插入排序后:" << endl; for(int i = 0; i < n; i++){ fout << g[i] << " "; } fout << endl; fout.close();
clock_t time15 = clock(); int *k = BS(a,0,n-1); clock_t time16 = clock(); fout.open("data.txt", ios::app); fout << "BS冒泡排序后:" << endl; for(int i = 0; i < n; i++){ fout << k[i] << " "; } fout << endl; fout.close();
fout.open("data.txt", ios::app); fout << "-----------------------------------------------" << endl; fout << "LSD基数排序用时:" << (double)(time2 - time1) / CLOCKS_PER_SEC << "s" << endl; fout << "MSD基数排序用时:" << (double)(time4 - time3) / CLOCKS_PER_SEC << "s" << endl; fout << "QS快速排序用时:" << (double)(time6 - time5) / CLOCKS_PER_SEC << "s" << endl; fout << "MS归并排序用时:" << (double)(time8 - time7) / CLOCKS_PER_SEC << "s" << endl; fout << "SS希尔排序用时:" << (double)(time10 - time9) / CLOCKS_PER_SEC << "s" << endl; fout << "IS插入排序用时:" << (double)(time12 - time11) / CLOCKS_PER_SEC << "s" << endl; fout.close(); return 0; }
|