我正在考虑生成mergesort中合并函数所需的所有索引,但我尝试了但是它没有很好用,因为我会得到负数的索引,因此它使程序崩溃。
以下是代码:
void myMergesort(void **A, int n, int(cmp)(void *, void*)) {
Stack *s;
s = stack_new();
int i, j;
int l = 0, h = n - 1;
int m = (l + h) / 2;
stack_push(s, (void *)l);
stack_push(s, (void *)h);
for (i = h; i > 0; i = i / 2) {
for (j = 0; j <= h ; j = j + i) {
stack_push(s, (void *)j);
}
if (j > h && j - i != h) {
stack_push(s, (void *)j);
}
}
while (stack_isEmpty(s)==0) {
h = (int)stack_pop(s);
l = (int)stack_pop(s);
m = (l + h) / 2;
if (l < h) {
merge(A, l, m, h, cmp);
}
}
}
我想知道我是否朝着正确的方向前进,如果我不能,有人会把我推向正确的方向吗?