void Merge(RedType R[],RedType T[],int low,int mid,int high)//归并排序 { int i=low,j=mid+1,k=low; while(i<=mid&&j<=high) { if(R[i].key<-R[j].key) T[k++]=R[i++]; else T[k++]=R[j++]; } while(i<=mid) T[k++]=R[i++]; while(j<=high) T[k++]=R[j++]; } void MSort(RedType R[],RedType T[],int low,int high) { RedType S[]={}; if(low==high) T[low]=R[low]; else { int mid=(low+high)/2; MSort(R,S,low,mid); MSort(R,S,mid+1,high); Merge(S,T,low,mid,high); } } void MergeSort(SqList &L)//归并排序 { MSort(L.r,L.r,1,L.length); }请问上述代码 该如何输出每一趟的排序结果?
时间: 2023-12-18 09:03:11 浏览: 157
要输出每一趟归并排序的结果,你可以在归并操作前后添加打印语句来实现。以下是修改后的代码示例:
```cpp
void Merge(RedType R[], RedType T[], int low, int mid, int high) // 归并排序
{
int i = low, j = mid + 1, k = low;
while (i <= mid && j <= high)
{
if (R[i].key < -R[j].key)
T[k++] = R[i++];
else
T[k++] = R[j++];
}
while (i <= mid)
T[k++] = R[i++];
while (j <= high)
T[k++] = R[j++];
// 输出当前趟的排序结果
for (int index = low; index <= high; index++)
{
R[index] = T[index];
cout << R[index].key << " ";
}
cout << endl;
}
void MSort(RedType R[], RedType T[], int low, int high)
{
RedType S[] = {};
if (low == high)
T[low] = R[low];
else
{
int mid = (low + high) / 2;
MSort(R, S, low, mid);
MSort(R, S, mid + 1, high);
Merge(S, T, low, mid, high);
}
}
void MergeSort(SqList &L) // 归并排序
{
MSort(L.r, L.r, 1, L.length);
}
```
在`Merge`函数中,添加了一个循环来输出当前趟的排序结果。在`MergeSort`函数中,调用了`MSort`函数来进行归并排序。每次归并操作后,都会输出当前趟的排序结果。
这样,你就可以看到每一趟归并排序的结果了。希望对你有帮助!
阅读全文