#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
void DisplayArray(int* a, int n) {
cout << "The elements in the array a[] are: \n";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
void BubbleSort(int* a, int n) {
for (int i = 0; i <= n - 2; i++) {
for (int j = 0; j <= n - 2 - i; j++) {
if (a[j] > a[j + 1]) {
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
void EfficientBubbleSort(int* a, int n) {
for (int i = 0; i <= n - 2; i++) {
bool flag = false;
for (int j = 0; j <= n - 2 - i; j++) {
if (a[j] > a[j + 1]) {
flag = true;
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
if (flag == false) {
break;
}
}
}
void InsertionSort(int* a, int n) {
for (int i = 1; i <= n - 1; i++) {
int key = a[i];
int k = i - 1;
for (; k >= 0; k--) {
if (a[k] > key) {
a[k + 1] = a[k];
}
else {
break;
}
}
a[k + 1] = key;
}
}
void Merge(int a[], int left, int middle, int right) {
int i1 = left;
int i2 = middle + 1;
int n = right - left + 1;
int* B = (int*)malloc(sizeof(int) * n);
int i = 0;
while (i1 <= middle && i2 <= right) {
if (a[i1] <= a[i2]) {
B[i] = a[i1];
i1++;
i++;
}
else {
B[i] = a[i2];
i2++;
i++;
}
}
while (i1 <= middle) {
B[i] = a[i1];
i1++;
i++;
}
while (i2 <= right) {
B[i] = a[i2];
i2++;
i++;
}
for (int j = 0; j < n; j++)
a[j + left] = B[j];
}
void MergeSort(int a[], int left, int right) {
if (left == right)
return;
int middle = (left + right) / 2;
MergeSort(a, left, middle);
MergeSort(a, middle + 1, right);
Merge(a, left, middle, right);
}
// 新的MergeSort3函数实现
void MergeSort3(int a[], int left, int right) {
if (left < right) {
int middle1 = left + (right - left) / 3;
int middle2 = left + 2 * (right - left) / 3;
MergeSort3(a, left, middle1);
MergeSort3(a, middle1 + 1, middle2);
MergeSort3(a, middle2 + 1, right);
// 分配临时数组空间
int n = right - left + 1;
int* temp = (int*)malloc(sizeof(int) * n);
int i = left, j = middle1 + 1, k = middle2 + 1, l = 0;
while (i <= middle1 && j <= middle2 && k <= right) {
if (a[i] <= a[j] && a[i] <= a[k]) {
temp[l++] = a[i++];
}
else if (a[j] <= a[i] && a[j] <= a[k]) {
temp[l++] = a[j++];
}
else {
temp[l++] = a[k++];
}
}
while (i <= middle1 && j <= middle2) {
if (a[i] <= a[j]) {
temp[l++] = a[i++];
}
else {
temp[l++] = a[j++];
}
}
while (i <= middle1 && k <= right) {
if (a[i] <= a[k]) {
temp[l++] = a[i++];
}
else {
temp[l++] = a[k++];
}
}
while (j <= middle2 && k <= right) {
if (a[j] <= a[k]) {
temp[l++] = a[j++];
}
else {
temp[l++] = a[k++];
}
}
while (i <= middle1) {
temp[l++] = a[i++];
}
while (j <= middle2) {
temp[l++] = a[j++];
}
while (k <= right) {
temp[l++] = a[k++];
}
// 将排序好的元素复制回原数组
for (i = left, l = 0; i <= right; i++, l++) {
a[i] = temp[l];
}
// 释放临时数组空间
free(temp);
}
}
int main() {
char Sorting_Algorithm[] = "MergeSort3";
int n = 20;
int* a = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++)
a[i] = rand() % n;
DisplayArray(a, n);
cout << "===========================================================\n";
if (strcmp(Sorting_Algorithm, "BubbleSort") == 0) {
cout << "Test the bubble sort.\n";
BubbleSort(a, n);
}
else if (strcmp(Sorting_Algorithm, "InsertionSort") == 0) {
cout << "Test the insertion sort.\n";
InsertionSort(a, n);
}
else if (strcmp(Sorting_Algorithm, "MergeSort") == 0) {
cout << "Test the merge sort.\n";
int left = 0;
int right = n - 1;
MergeSort(a, left, right);
}
else if (strcmp(Sorting_Algorithm, "MergeSort3") == 0) {
cout << "Test the merge sort 3.\n";
int left = 0;
int right = n - 1;
MergeSort3(a, left, right);
}
DisplayArray(a, n);
cout << "===========================================================\n";
free(a);
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHN0ZGxpYi5oPiAKI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RyaW5nLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBEaXNwbGF5QXJyYXkoaW50KiBhLCBpbnQgbikgewogICAgY291dCA8PCAiVGhlIGVsZW1lbnRzIGluIHRoZSBhcnJheSBhW10gYXJlOiBcbiI7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBjb3V0IDw8IGFbaV0gPDwgIiAiOwogICAgY291dCA8PCBlbmRsOwp9Cgp2b2lkIEJ1YmJsZVNvcnQoaW50KiBhLCBpbnQgbikgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbiAtIDI7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDw9IG4gLSAyIC0gaTsgaisrKSB7CiAgICAgICAgICAgIGlmIChhW2pdID4gYVtqICsgMV0pIHsKICAgICAgICAgICAgICAgIGludCB0bXAgPSBhW2pdOwogICAgICAgICAgICAgICAgYVtqXSA9IGFbaiArIDFdOwogICAgICAgICAgICAgICAgYVtqICsgMV0gPSB0bXA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCnZvaWQgRWZmaWNpZW50QnViYmxlU29ydChpbnQqIGEsIGludCBuKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8PSBuIC0gMjsgaSsrKSB7CiAgICAgICAgYm9vbCBmbGFnID0gZmFsc2U7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPD0gbiAtIDIgLSBpOyBqKyspIHsKICAgICAgICAgICAgaWYgKGFbal0gPiBhW2ogKyAxXSkgewogICAgICAgICAgICAgICAgZmxhZyA9IHRydWU7CiAgICAgICAgICAgICAgICBpbnQgdG1wID0gYVtqXTsKICAgICAgICAgICAgICAgIGFbal0gPSBhW2ogKyAxXTsKICAgICAgICAgICAgICAgIGFbaiArIDFdID0gdG1wOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChmbGFnID09IGZhbHNlKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0KfQoKdm9pZCBJbnNlcnRpb25Tb3J0KGludCogYSwgaW50IG4pIHsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG4gLSAxOyBpKyspIHsKICAgICAgICBpbnQga2V5ID0gYVtpXTsKICAgICAgICBpbnQgayA9IGkgLSAxOwogICAgICAgIGZvciAoOyBrID49IDA7IGstLSkgewogICAgICAgICAgICBpZiAoYVtrXSA+IGtleSkgewogICAgICAgICAgICAgICAgYVtrICsgMV0gPSBhW2tdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgYVtrICsgMV0gPSBrZXk7CiAgICB9Cn0KCnZvaWQgTWVyZ2UoaW50IGFbXSwgaW50IGxlZnQsIGludCBtaWRkbGUsIGludCByaWdodCkgewogICAgaW50IGkxID0gbGVmdDsKICAgIGludCBpMiA9IG1pZGRsZSArIDE7CiAgICBpbnQgbiA9IHJpZ2h0IC0gbGVmdCArIDE7CgogICAgaW50KiBCID0gKGludCopbWFsbG9jKHNpemVvZihpbnQpICogbik7CiAgICBpbnQgaSA9IDA7CgogICAgd2hpbGUgKGkxIDw9IG1pZGRsZSAmJiBpMiA8PSByaWdodCkgewogICAgICAgIGlmIChhW2kxXSA8PSBhW2kyXSkgewogICAgICAgICAgICBCW2ldID0gYVtpMV07CiAgICAgICAgICAgIGkxKys7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIEJbaV0gPSBhW2kyXTsKICAgICAgICAgICAgaTIrKzsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0KICAgIH0KCiAgICB3aGlsZSAoaTEgPD0gbWlkZGxlKSB7CiAgICAgICAgQltpXSA9IGFbaTFdOwogICAgICAgIGkxKys7CiAgICAgICAgaSsrOwogICAgfQoKICAgIHdoaWxlIChpMiA8PSByaWdodCkgewogICAgICAgIEJbaV0gPSBhW2kyXTsKICAgICAgICBpMisrOwogICAgICAgIGkrKzsKICAgIH0KCiAgICBmb3IgKGludCBqID0gMDsgaiA8IG47IGorKykKICAgICAgICBhW2ogKyBsZWZ0XSA9IEJbal07Cn0KCnZvaWQgTWVyZ2VTb3J0KGludCBhW10sIGludCBsZWZ0LCBpbnQgcmlnaHQpIHsKICAgIGlmIChsZWZ0ID09IHJpZ2h0KQogICAgICAgIHJldHVybjsKICAgIGludCBtaWRkbGUgPSAobGVmdCArIHJpZ2h0KSAvIDI7CiAgICBNZXJnZVNvcnQoYSwgbGVmdCwgbWlkZGxlKTsKICAgIE1lcmdlU29ydChhLCBtaWRkbGUgKyAxLCByaWdodCk7CiAgICBNZXJnZShhLCBsZWZ0LCBtaWRkbGUsIHJpZ2h0KTsKfQoKLy8g5paw55qETWVyZ2VTb3J0M+WHveaVsOWunueOsAp2b2lkIE1lcmdlU29ydDMoaW50IGFbXSwgaW50IGxlZnQsIGludCByaWdodCkgewogICAgaWYgKGxlZnQgPCByaWdodCkgewogICAgICAgIGludCBtaWRkbGUxID0gbGVmdCArIChyaWdodCAtIGxlZnQpIC8gMzsKICAgICAgICBpbnQgbWlkZGxlMiA9IGxlZnQgKyAyICogKHJpZ2h0IC0gbGVmdCkgLyAzOwoKICAgICAgICBNZXJnZVNvcnQzKGEsIGxlZnQsIG1pZGRsZTEpOwogICAgICAgIE1lcmdlU29ydDMoYSwgbWlkZGxlMSArIDEsIG1pZGRsZTIpOwogICAgICAgIE1lcmdlU29ydDMoYSwgbWlkZGxlMiArIDEsIHJpZ2h0KTsKCiAgICAgICAgLy8g5YiG6YWN5Li05pe25pWw57uE56m66Ze0CiAgICAgICAgaW50IG4gPSByaWdodCAtIGxlZnQgKyAxOwogICAgICAgIGludCogdGVtcCA9IChpbnQqKW1hbGxvYyhzaXplb2YoaW50KSAqIG4pOwoKICAgICAgICBpbnQgaSA9IGxlZnQsIGogPSBtaWRkbGUxICsgMSwgayA9IG1pZGRsZTIgKyAxLCBsID0gMDsKCiAgICAgICAgd2hpbGUgKGkgPD0gbWlkZGxlMSAmJiBqIDw9IG1pZGRsZTIgJiYgayA8PSByaWdodCkgewogICAgICAgICAgICBpZiAoYVtpXSA8PSBhW2pdICYmIGFbaV0gPD0gYVtrXSkgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtpKytdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKGFbal0gPD0gYVtpXSAmJiBhW2pdIDw9IGFba10pIHsKICAgICAgICAgICAgICAgIHRlbXBbbCsrXSA9IGFbaisrXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgIHRlbXBbbCsrXSA9IGFbaysrXTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGkgPD0gbWlkZGxlMSAmJiBqIDw9IG1pZGRsZTIpIHsKICAgICAgICAgICAgaWYgKGFbaV0gPD0gYVtqXSkgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtpKytdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtqKytdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoaSA8PSBtaWRkbGUxICYmIGsgPD0gcmlnaHQpIHsKICAgICAgICAgICAgaWYgKGFbaV0gPD0gYVtrXSkgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtpKytdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtrKytdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoaiA8PSBtaWRkbGUyICYmIGsgPD0gcmlnaHQpIHsKICAgICAgICAgICAgaWYgKGFbal0gPD0gYVtrXSkgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtqKytdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtrKytdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoaSA8PSBtaWRkbGUxKSB7CiAgICAgICAgICAgIHRlbXBbbCsrXSA9IGFbaSsrXTsKICAgICAgICB9CgogICAgICAgIHdoaWxlIChqIDw9IG1pZGRsZTIpIHsKICAgICAgICAgICAgdGVtcFtsKytdID0gYVtqKytdOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGsgPD0gcmlnaHQpIHsKICAgICAgICAgICAgdGVtcFtsKytdID0gYVtrKytdOwogICAgICAgIH0KCiAgICAgICAgLy8g5bCG5o6S5bqP5aW955qE5YWD57Sg5aSN5Yi25Zue5Y6f5pWw57uECiAgICAgICAgZm9yIChpID0gbGVmdCwgbCA9IDA7IGkgPD0gcmlnaHQ7IGkrKywgbCsrKSB7CiAgICAgICAgICAgIGFbaV0gPSB0ZW1wW2xdOwogICAgICAgIH0KCiAgICAgICAgLy8g6YeK5pS+5Li05pe25pWw57uE56m66Ze0CiAgICAgICAgZnJlZSh0ZW1wKTsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBjaGFyIFNvcnRpbmdfQWxnb3JpdGhtW10gPSAiTWVyZ2VTb3J0MyI7IAoKICAgIGludCBuID0gMjA7IAoKICAgIGludCogYSA9IChpbnQqKW1hbGxvYyhzaXplb2YoaW50KSAqIG4pOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGFbaV0gPSByYW5kKCkgJSBuOyAKCiAgICBEaXNwbGF5QXJyYXkoYSwgbik7IAoKICAgIGNvdXQgPDwgIj09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XG4iOwoKICAgIGlmIChzdHJjbXAoU29ydGluZ19BbGdvcml0aG0sICJCdWJibGVTb3J0IikgPT0gMCkgewogICAgICAgIGNvdXQgPDwgIlRlc3QgdGhlIGJ1YmJsZSBzb3J0LlxuIjsKICAgICAgICBCdWJibGVTb3J0KGEsIG4pOwogICAgfQogICAgZWxzZSBpZiAoc3RyY21wKFNvcnRpbmdfQWxnb3JpdGhtLCAiSW5zZXJ0aW9uU29ydCIpID09IDApIHsKICAgICAgICBjb3V0IDw8ICJUZXN0IHRoZSBpbnNlcnRpb24gc29ydC5cbiI7CiAgICAgICAgSW5zZXJ0aW9uU29ydChhLCBuKTsKICAgIH0KICAgIGVsc2UgaWYgKHN0cmNtcChTb3J0aW5nX0FsZ29yaXRobSwgIk1lcmdlU29ydCIpID09IDApIHsKICAgICAgICBjb3V0IDw8ICJUZXN0IHRoZSBtZXJnZSBzb3J0LlxuIjsKICAgICAgICBpbnQgbGVmdCA9IDA7CiAgICAgICAgaW50IHJpZ2h0ID0gbiAtIDE7CiAgICAgICAgTWVyZ2VTb3J0KGEsIGxlZnQsIHJpZ2h0KTsKICAgIH0KICAgIGVsc2UgaWYgKHN0cmNtcChTb3J0aW5nX0FsZ29yaXRobSwgIk1lcmdlU29ydDMiKSA9PSAwKSB7CiAgICAgICAgY291dCA8PCAiVGVzdCB0aGUgbWVyZ2Ugc29ydCAzLlxuIjsKICAgICAgICBpbnQgbGVmdCA9IDA7CiAgICAgICAgaW50IHJpZ2h0ID0gbiAtIDE7CiAgICAgICAgTWVyZ2VTb3J0MyhhLCBsZWZ0LCByaWdodCk7CiAgICB9CgogICAgRGlzcGxheUFycmF5KGEsIG4pOwoKICAgIGNvdXQgPDwgIj09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XG4iOwoKICAgIGZyZWUoYSk7CgogICAgcmV0dXJuIDA7Cn0=