gpt4 book ai didi

c - 检测到堆损坏 - 使用字符串实现合并排序

转载 作者:行者123 更新时间:2023-11-30 20:55:43 25 4
gpt4 key购买 nike

之前我自己实现了一个“经典”合并排序,对 int 数组进行排序。按照代码:

#include <stdio.h>
#include <stdlib.h>

void merge(int a[], int b[], int c[], int m, int n)
{
int i = 0, j = 0, k = 0;

while (i < m && j < n)
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
while (i < m)
c[k++] = a[i++];
while (j < n)
c[k++] = b[j++];
}

void mergesort_pot2(int key[], int n)
{
int j, k, *w;

w = calloc(n, sizeof(int));
for (k = 1; k < n; k *= 2) {
for (j = 0; j < n - k; j += 2 * k)
merge(key + j, key + j + k, w + j, k, k);
for (j = 0; j < n; ++j)
key[j] = w[j];
}
free(w);
}

void mergesort(int key[], int n)
{
int i, cnt = 0, *x, v, b = 0, vv = 0, bb;

while (n - cnt > 0)
{
for (i = 1; i < n - cnt; i *= 2);
if (i > n - cnt)
i /= 2;

x = calloc(i, sizeof(int));

v = 0;
while (v < i)
x[v++] = key[b++];

mergesort_pot2(x, i);

bb = 0;
while (bb < i)
key[vv++] = x[bb++];

free(x);
cnt += i;
}
}

int main(void)
{
int i, key[] = { 4, 3, 1, 67, 55, 8, 0, 4,
-5, 37, 7, 4, 2, 9, 1, -1, 100};

printf("Before mergesort:\n");
for (i = 0; i < 17; ++i)
printf("%4d", key[i]);
putchar('\n');

mergesort(key, 17);

printf("After mergesort:\n");
for (i = 0; i < 17; ++i)
printf("%4d", key[i]);
putchar('\n');
return 0;
}

现在,作为练习,我调整了该代码以使其能够与 char* 数组一起使用,但我在某处遇到了堆错误。我尝试使用 Dr. Memory 对其进行调试,但它破坏了我的 Visual Studio 2013,无法正常工作,但这是另一个故事了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LENGTH 5

void merge(char *a[], char *b[], char *c[], int m, int n)
{
int i = 0, j = 0, k = 0;

while (i < m && j < n)
if (strcmp(a[i], b[j]) < 0)
c[k++] = a[i++];
else
c[k++] = b[j++];
while (i < m)
c[k++] = a[i++];
while (j < n)
c[k++] = b[j++];
}

void mergesort_pot2(char *key[], int n)
{
int j, k;
char **w;
int i;

w = calloc(n, sizeof(char *));
for (i = 0; i < n; ++i)
w[i] = calloc(strlen(key[i]), sizeof(char));

for (k = 1; k < n; k *= 2) {
for (j = 0; j < n - k; j += 2 * k)
merge(key + j, key + j + k, w + j, k, k);
for (j = 0; j < n; ++j)
key[j] = w[j];
}
for (i = 0; i < n; ++i)
free(w[i]);
free(w);
}

void mergesort(char *key[], int n)
{
int i, cnt = 0, v, b = 0, vv = 0, bb;
char **x;
int y;

while (n - cnt > 0)
{
for (i = 1; i < n - cnt; i *= 2);
if (i > n - cnt)
i /= 2;

x = calloc(i, sizeof(char *));

for (y = 0; y < i; ++y)
x[y] = calloc(strlen(key[y]), sizeof(char));
v = 0;
while (v < i)
strcpy(x[v++], key[b++]);

mergesort_pot2(x, i);

bb = 0;
while (bb < i)
strcpy(key[vv++], x[bb++]);

for (y = 0; y < i; ++y)
free(x[y]);
free(x);

cnt += i;
}
}

int main(void)
{
int i;
char *key[LENGTH] = { "Hi", "this", "is", "a", "test" };

printf("Before mergesort:\n");
for (i = 0; i < LENGTH; ++i)
printf("%s ", key[i]);
putchar('\n');

mergesort(key, LENGTH);

printf("After mergesort:\n");
for (i = 0; i < LENGTH; ++i)
printf("%s ", key[i]);
putchar('\n');
return 0;
}

最佳答案

void mergesort_pot2(char *key[], int n)
{
int j, k;
char **w;

w = calloc(n, sizeof(char*));
for (k = 1; k < n; k *= 2) {
for (j = 0; j < n - k; j += 2 * k)
merge(key + j, key + j + k, w + j, k, k);
for (j = 0; j < n; ++j)
key[j] = w[j];
}
free(w);
}

void mergesort(char *key[], int n)
{
int i, cnt = 0, v, b = 0, vv = 0, bb;
char **x;

while (n - cnt > 0)
{
for (i = 1; i < n - cnt; i *= 2);
if (i > n - cnt)
i /= 2;

x = calloc(i, sizeof(char *));

v = 0;
while (v < i)
x[v++] = key[b++];

mergesort_pot2(x, i);

bb = 0;
while (bb < i)
key[vv++]= x[bb++];

free(x);

cnt += i;
}
}

关于c - 检测到堆损坏 - 使用字符串实现合并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com