#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];
free(B);
}
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+IGtleSkgewogICAgICAgICAgICAgICAgYVtrICsgMV0gPSBhW2tdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgYVtrICsgMV0gPSBrZXk7CiAgICB9Cn0KCnZvaWQgTWVyZ2UoaW50IGFbXSwgaW50IGxlZnQsIGludCBtaWRkbGUsIGludCByaWdodCkgewogICAgaW50IGkxID0gbGVmdDsKICAgIGludCBpMiA9IG1pZGRsZSArIDE7CiAgICBpbnQgbiA9IHJpZ2h0IC0gbGVmdCArIDE7CgogICAgaW50KiBCID0gKGludCopbWFsbG9jKHNpemVvZihpbnQpICogbik7CiAgICBpbnQgaSA9IDA7CgogICAgd2hpbGUgKGkxIDw9IG1pZGRsZSAmJiBpMiA8PSByaWdodCkgewogICAgICAgIGlmIChhW2kxXSA8PSBhW2kyXSkgewogICAgICAgICAgICBCW2ldID0gYVtpMV07CiAgICAgICAgICAgIGkxKys7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIEJbaV0gPSBhW2kyXTsKICAgICAgICAgICAgaTIrKzsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0KICAgIH0KCiAgICB3aGlsZSAoaTEgPD0gbWlkZGxlKSB7CiAgICAgICAgQltpXSA9IGFbaTFdOwogICAgICAgIGkxKys7CiAgICAgICAgaSsrOwogICAgfQoKICAgIHdoaWxlIChpM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gbWlkZGxlMiArIDEsIGwgPSAwOwoKICAgICAgICB3aGlsZSAoaSA8PSBtaWRkbGUxICYmIGogPD0gbWlkZGxlMiAmJiBrIDw9IHJpZ2h0KSB7CiAgICAgICAgICAgIGlmIChhW2ldIDw9IGFbal0gJiYgYVtpXSA8PSBhW2tdKSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2krK107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAoYVtqXSA8PSBhW2ldICYmIGFbal0gPD0gYVtrXSkgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtqKytdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgdGVtcFtsKytdID0gYVtrKytdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoaSA8PSBtaWRkbGUxICYmIGogPD0gbWlkZGxlMikgewogICAgICAgICAgICBpZiAoYVtpXSA8PSBhW2pdKSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2krK107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2orK107CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHdoaWxlIChpIDw9IG1pZGRsZTEgJiYgayA8PSByaWdodCkgewogICAgICAgICAgICBpZiAoYVtpXSA8PSBhW2tdKSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2krK107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2srK107CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHdoaWxlIChqIDw9IG1pZGRsZTIgJiYgayA8PSByaWdodCkgewogICAgICAgICAgICBpZiAoYVtqXSA8PSBhW2tdKSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2orK107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2srK107CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHdoaWxlIChpIDw9IG1pZGRsZTEpIHsKICAgICAgICAgICAgdGVtcFtsKytdID0gYVtpKytdOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGogPD0gbWlkZGxlMikgewogICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2orK107CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoayA8PSByaWdodCkgewogICAgICAgICAgICB0ZW1wW2wrK10gPSBhW2srK107CiAgICAgICAgfQoKICAgICAgICBmb3IgKGkgPSBsZWZ0LCBsID0gMDsgaSA8PSByaWdodDsgaSsrLCBsKyspIHsKICAgICAgICAgICAgYVtpXSA9IHRlbXBbbF07CiAgICAgICAgfQoKICAgICAgICBmcmVlKHRlbXApOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGNoYXIgU29ydGluZ19BbGdvcml0aG1bXSA9ICJNZXJnZVNvcnQzIjsgCgogICAgaW50IG4gPSAyMDsgCgogICAgaW50KiBhID0gKGludCopbWFsbG9jKHNpemVvZihpbnQpICogbik7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgYVtpXSA9IHJhbmQoKSAlIG47IAoKICAgIERpc3BsYXlBcnJheShhLCBuKTsgCgogICAgY291dCA8PCAiPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT1cbiI7CgogICAgaWYgKHN0cmNtcChTb3J0aW5nX0FsZ29yaXRobSwgIkJ1YmJsZVNvcnQiKSA9PSAwKSB7CiAgICAgICAgY291dCA8PCAiVGVzdCB0aGUgYnViYmxlIHNvcnQuXG4iOwogICAgICAgIEJ1YmJsZVNvcnQoYSwgbik7CiAgICB9CiAgICBlbHNlIGlmIChzdHJjbXAoU29ydGluZ19BbGdvcml0aG0sICJJbnNlcnRpb25Tb3J0IikgPT0gMCkgewogICAgICAgIGNvdXQgPDwgIlRlc3QgdGhlIGluc2VydGlvbiBzb3J0LlxuIjsKICAgICAgICBJbnNlcnRpb25Tb3J0KGEsIG4pOwogICAgfQogICAgZWxzZSBpZiAoc3RyY21wKFNvcnRpbmdfQWxnb3JpdGhtLCAiTWVyZ2VTb3J0IikgPT0gMCkgewogICAgICAgIGNvdXQgPDwgIlRlc3QgdGhlIG1lcmdlIHNvcnQuXG4iOwogICAgICAgIGludCBsZWZ0ID0gMDsKICAgICAgICBpbnQgcmlnaHQgPSBuIC0gMTsKICAgICAgICBNZXJnZVNvcnQoYSwgbGVmdCwgcmlnaHQpOwogICAgfQogICAgZWxzZSBpZiAoc3RyY21wKFNvcnRpbmdfQWxnb3JpdGhtLCAiTWVyZ2VTb3J0MyIpID09IDApIHsKICAgICAgICBjb3V0IDw8ICJUZXN0IHRoZSBtZXJnZSBzb3J0IDMuXG4iOwogICAgICAgIGludCBsZWZ0ID0gMDsKICAgICAgICBpbnQgcmlnaHQgPSBuIC0gMTsKICAgICAgICBNZXJnZVNvcnQzKGEsIGxlZnQsIHJpZ2h0KTsKICAgIH0KCiAgICBEaXNwbGF5QXJyYXkoYSwgbik7CgogICAgY291dCA8PCAiPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT1cbiI7CgogICAgZnJlZShhKTsKCiAgICByZXR1cm4gMDsKfQ==