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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
| #include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<iostream> #include<windows.h> #include<time.h> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 10 #define LISTINCREMRNT 10 typedef int Status; typedef int ElemType; typedef struct { ElemType* elem; int length; int listsize; }SqList;
Status InitList(SqList* L) {
L->elem = (ElemType*)__vcrt_malloc_normal(LIST_INIT_SIZE * sizeof(ElemType)); if (!(L->elem)) exit(OVERFLOW); L->length = 0; L->listsize = LIST_INIT_SIZE;
return OK; }
Status ListInsert(SqList* L, int i, ElemType e) { if (i<1 || i>L->length + 1) return ERROR;
ElemType* q = &(L->elem[i - 1]), *p = NULL; for (p = &(L->elem[L->length - 1]); p >= q; --p) * (p + 1) = *p; *q = e; ++(L->length); if (L->length == L->listsize) { ElemType* newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMRNT) * sizeof(ElemType)); if (!newbase) exit(OVERFLOW); L->elem = newbase; L->listsize += LISTINCREMRNT; } return OK; }
int getLength(SqList* L) { return L->length; }
char* isEmtey(SqList* L) { return L->length == 0 ? "空" : "不空"; }
Status clearList(SqList* L) { __vcrt_free_normal(L->elem); L->length = 0; return OK; }
Status destryList(SqList* L) {
__vcrt_free_normal(L); L = NULL; return OK; }
ElemType getElemByIndex(SqList* L, int i) { return L->elem[i - 1]; }
int getIndexByElem(SqList* L, ElemType e) { for (int j = 0; j < L->length; ++j) { if ((L->elem[j]) == e) return j + 1; } return -1; }
ElemType changeElemByIndex(SqList* L, int i, ElemType e) { return L->elem[i - 1] = e;
}
Status myPrintf(SqList* L) { for (int i = 0; i < L->length; i++) { printf("%d ",(L->elem[i])); } return OK; }
int verify(SqList* L) { int length = getLength(L);
for (int i = 0; i < length-1; i++) { if (L->elem[i]>L->elem[i+1]) { return 0; } } return 1; }
SqList* createSqByCin() { SqList* L = (SqList*)__vcrt_malloc_normal(sizeof(SqList)); InitList(L); int n = 0; cin >> n; cout << "输入数据:"; ElemType elem = NULL; for (int i = 0; i < n; i++) { cin >> elem; ListInsert(L, i + 1, elem); } cout << "建表成功: "; myPrintf(L); cout << endl; return L; }
SqList* createSqByRandom() { SqList* L = (SqList*)__vcrt_malloc_normal(sizeof(SqList)); InitList(L); int n = 0; cin >> n; ElemType elem = NULL; for (int i = 0; i < n; i++) { ListInsert(L, i + 1, rand()%100+1); } cout << "建表成功: "; myPrintf(L); cout << endl; return L; }
void InsertSort(SqList* L) { int j = 0; int length = getLength(L); _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); for (int i = 1; i < length; i++) { ElemType temp = L->elem[i]; for (j= i-1; j >=0&&L->elem[j]>temp; j--) { L->elem[j + 1] = L->elem[j]; } L->elem[j + 1] = temp; } QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000*(finish.QuadPart - start.QuadPart)/Freq <<" 微秒" <<endl; cout << endl; destryList(L); }
void ShellInsert(SqList* L,int dk) { int j = 0; int length = getLength(L); for (int i = dk; i < length; i++) { j = i - dk; ElemType temp = L->elem[i]; while ( j >= 0 && L->elem[j]>temp) { L->elem[j + dk] = L->elem[j]; j -= dk; } if (j!=i-dk) { L->elem[j + dk] = temp; } } } void ShellSort(SqList* L) { int dk = getLength(L)/2; _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); while ( dk>=1) { ShellInsert(L, dk); dk = dk / 2; } QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
void BubbleSort(SqList* L) { int length = getLength(L); int flag = 1; _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); for (int i = 0; i < length&&flag==1; i++) { flag = 0; for (int j = 0; j < length-i-1; j++) { if (L->elem[j]>L->elem[j+1]) { flag = 1; L->elem[j + 1] = L->elem[j] ^ L->elem[j + 1]; L->elem[j] = L->elem[j] ^ L->elem[j + 1]; L->elem[j + 1] = L->elem[j] ^ L->elem[j + 1]; } } } QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
int Partition(SqList* L, int low, int high) { ElemType pivotkey = L->elem[low]; while (low<high) { while (low<high&&L->elem[high]>=pivotkey) { high--; } L->elem[low] = L->elem[high]; while (low<high&&L->elem[low]<=pivotkey) { low++; } L->elem[high] = L->elem[low]; } L->elem[low] = pivotkey; return low; } void QSort(SqList* L, int low, int high) { if (low < high) { int pivotloc = Partition(L, low, high); QSort(L, low, pivotloc - 1); QSort(L, pivotloc + 1, high); } } void QuickSort(SqList* L) { int length = getLength(L); _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); QSort(L, 0, length-1); QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
void SelectSort(SqList* L) { int length = getLength(L)-1; _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); for (int i = 0; i < length; i++) { int index = i; for (int j = i+1; j <= length; j++) { if (L->elem[j] < L->elem[index]) { index = j; } } if (L->elem[index]!= L->elem[i]) { L->elem[index] = L->elem[index] ^ L->elem[i]; L->elem[i] = L->elem[index] ^ L->elem[i]; L->elem[index] = L->elem[index] ^ L->elem[i]; } } QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
void heapify(ElemType elem[], int i, int n)
{
int c1 = 2 * i + 1; int c2 = 2 * i + 2; int max = i; if (c1<n&&elem[c1]>elem[max]) max = c1; if (c2<n&&elem[c2]>elem[max]) max = c2; if (max != i) { elem[i] = elem[i] ^ elem[max]; elem[max] = elem[i] ^ elem[max]; elem[i] = elem[i] ^ elem[max]; heapify(elem, max, n); } } void CreatHeap(SqList* L,int n)
{ int lost_node = (n - 1) ; for (int i = (lost_node-1)/2; i >= 0; --i) { heapify(L->elem, i, n); } cout << "创建的初堆:"; myPrintf(L); cout << endl; } void HeapSort(SqList* L) { int n = getLength(L); _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); CreatHeap(L,n); for(int i = n-1; i >= 0; --i) { if (L->elem[0]!=L->elem[i]) { L->elem[i] = L->elem[i] ^ L->elem[0]; L->elem[0] = L->elem[i] ^ L->elem[0]; L->elem[i] = L->elem[i] ^ L->elem[0]; } heapify(L->elem, 0, i); } QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
void Merge(ElemType elem[], int start, int mid, int end) { ElemType *tmp = (ElemType *)malloc((end - start + 1) * sizeof(ElemType)); int i = start; int j = mid + 1; int k = 0; while (i <= mid && j <= end) { if ( elem[i] <= elem[j]) tmp[k++] = elem[i++]; else tmp[k++] = elem[j++]; } while (i <= mid) tmp[k++] = elem[i++];
while (j <= end) tmp[k++] = elem[j++]; for (i = 0; i < k; i++) elem[start + i] = tmp[i]; free(tmp); } void merge_sort_up2down(ElemType elem[], int start, int end) { if (elem == NULL || start >= end) return;
int mid = (end + start) / 2; merge_sort_up2down(elem, start, mid); merge_sort_up2down(elem, mid + 1, end); Merge(elem, start, mid, end); } void MergeSort(SqList* L) { int end = getLength(L) - 1; _LARGE_INTEGER start, finish; double Freq; LARGE_INTEGER f; QueryPerformanceFrequency(&f); Freq = (double)f.QuadPart; QueryPerformanceCounter(&start); merge_sort_up2down(L->elem, 0, end); QueryPerformanceCounter(&finish); if (verify(L)) { cout << "通过" << endl; } else { cout << "不通过" << endl; } cout << "排序后:"; myPrintf(L); cout << " 共用时: " << 1000000 * (finish.QuadPart - start.QuadPart) / Freq << " 微秒" << endl; cout << endl; destryList(L); }
void Mode1() { SqList* Sq = NULL; char ch = NULL; printf("进入模式一\n" "1.插入排序\n" "2.希尔排序\n" "3.冒泡排序\n" "4.快速排序\n" "5.选择排序\n" "6.堆排序\n" "7.归并排序\n"); while (true) { cin >> ch; switch (ch) { case'1':cout << "1、输入个数:"; Sq = createSqByCin(); InsertSort(Sq); break; case'2':cout << "2、输入个数:"; Sq = createSqByCin(); ShellSort(Sq); break; case'3':cout << "3、输入个数:"; Sq = createSqByCin(); BubbleSort(Sq); break; case'4':cout << "4、输入个数:"; Sq = createSqByCin(); QuickSort(Sq); break; case'5':cout << "5、输入个数:"; Sq = createSqByCin(); SelectSort(Sq); break; case'6':cout << "6、输入个数:"; Sq = createSqByCin(); HeapSort(Sq); break; case'7':cout << "7、输入个数:"; Sq = createSqByCin(); MergeSort(Sq); break; default:return; break; } } }
void Mode2() { SqList* Sq = NULL; char ch = NULL; printf("进入模式二\n" "1.插入排序\n" "2.希尔排序\n" "3.冒泡排序\n" "4.快速排序\n" "5.选择排序\n" "6.堆排序\n" "7.归并排序\n"); while (true) { cin >> ch; switch (ch) { case'1':cout << "1、输入个数:"; Sq = createSqByRandom(); InsertSort(Sq); break; case'2':cout << "2、输入个数:"; Sq = createSqByRandom(); ShellSort(Sq); break; case'3':cout << "3、输入个数:"; Sq = createSqByRandom(); BubbleSort(Sq); break; case'4':cout << "4、输入个数:"; Sq = createSqByRandom(); QuickSort(Sq); break; case'5':cout << "5、输入个数:"; Sq = createSqByRandom(); SelectSort(Sq); break; case'6':cout << "6、输入个数:"; Sq = createSqByRandom(); HeapSort(Sq); break; case'7':cout << "7、输入个数:"; Sq = createSqByRandom(); MergeSort(Sq); break; default:return; break; } } } int main(void) { srand((unsigned int)time(NULL)); SqList* Sq = NULL; char ch = NULL; printf("选择模式:\n1.:输入模式\n2.:随机模式\n"); while (true) { cin >> ch; switch (ch) { case '1':Mode1(); break; case '2':Mode2(); break; default:system("pause"); return 0; break; } } system("pause"); return 0; }
|