#include<iostream>using namespace std;void merge(int num[], int tempnum[], int L, int R);void mergesourt(int num[], int tempnum[], int L, int R);int _tmain(int argc, _TCHAR* argv[]){ int a[6] = { 1, 6, 5, 9, 8, 7 }; int b[6]; mergesourt(a, b, 0, 5); for (int i = 0; i < 6; i++) { cout << a[i] << " "; } cout << endl; return 0;}void merge(int num[], int tempnum[], int L, int R){ int i = 0,j=L,k=R; int mid = R - ((R - L) >> 1); while (j <= mid&&k >= mid + 1) { tempnum[i++] = (num[j] < num[k]) ? num[j++] : num[k++]; } while (j <= mid) { tempnum[i++] = num[j++]; } while (k >= mid + 1) { tempnum[i++] = num[k++]; } for (i = L; i <= R; i++) { num[i] = tempnum[i]; }}void mergesourt(int num[], int tempnum[], int L, int R){ int mid = R - ((R - L) >> 1); if (L < R) { mergesourt(num, tempnum, L, mid); mergesourt(num,tempnum, mid + 1, R); merge(num, tempnum, L, R); }}
1 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
这个mergesourt函数啊,当L=0,R=1的时候,mid是1,因此会陷入无限递归
(为何不使用L+(R-L)/2呢?)
另外,merge函数中为什么是k>=mid+1?这样循环就不会停啊?
- 1 回答
- 0 关注
- 643 浏览
添加回答
举报
0/150
提交
取消