|
|
@ -8,7 +8,7 @@ void print(int a[], int n) {
|
|
|
|
cout << endl;
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>r[i<><69>m]<5D><>r[m +1 <20><>n]<5D>鲢<EFBFBD><E9B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rf[i<><69>n]
|
|
|
|
//将r[i...m]和r[m+1...n]归并到辅助数组rf[i...n]
|
|
|
|
void Merge(ElemType *r, ElemType *rf, int i, int m, int n)
|
|
|
|
void Merge(ElemType *r, ElemType *rf, int i, int m, int n)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int j, k;
|
|
|
|
int j, k;
|
|
|
@ -31,13 +31,13 @@ void MergeSort(ElemType *r, ElemType *rf, int lenght)
|
|
|
|
len = 2 * s;
|
|
|
|
len = 2 * s;
|
|
|
|
int i = 0;
|
|
|
|
int i = 0;
|
|
|
|
while (i + len < lenght) {
|
|
|
|
while (i + len < lenght) {
|
|
|
|
Merge(q, rf, i, i + s - 1, i + len - 1); //<2F>Եȳ<D4B5><C8B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><D3B1>ϲ<EFBFBD>
|
|
|
|
Merge(q, rf, i, i + s - 1, i + len - 1);
|
|
|
|
i = i + len;
|
|
|
|
i = i + len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i + s < lenght) {
|
|
|
|
if (i + s < lenght) {
|
|
|
|
Merge(q, rf, i, i + s - 1, lenght - 1); //<2F>Բ<EFBFBD><D4B2>ȳ<EFBFBD><C8B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><D3B1>ϲ<EFBFBD>
|
|
|
|
Merge(q, rf, i, i + s - 1, lenght - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp = q; q = rf; rf = tmp; //<2F><><EFBFBD><EFBFBD>q,rf<72><66><EFBFBD>Ա<EFBFBD>֤<EFBFBD><D6A4>һ<EFBFBD>˹鲢ʱ<E9B2A2><CAB1><EFBFBD>Դ<EFBFBD>q <20>鲢<EFBFBD><E9B2A2>rf
|
|
|
|
tmp = q; q = rf; rf = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -47,7 +47,7 @@ int main() {
|
|
|
|
int b[10];
|
|
|
|
int b[10];
|
|
|
|
MergeSort(a, b, 10);
|
|
|
|
MergeSort(a, b, 10);
|
|
|
|
print(b, 10);
|
|
|
|
print(b, 10);
|
|
|
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
|
|
|
cout << "结果:";
|
|
|
|
print(a, 10);
|
|
|
|
print(a, 10);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|