为什么堆排序的 C 实现会出现分段错误?

发布于 2024-12-17 08:06:34 字数 1702 浏览 2 评论 0原文

我尝试在 gcc 和turboc 中执行此代码。在 GCC 中,它在运行时给出了分段错误错误,而在 Turbo 中,它在运行时再次给出了空指针分配错误。

我尝试追踪它,但我看不出问题出在哪里。

#include<stdio.h>

int heapsize;

void heapify(int a[],int i)
{
    int l=2*i,r=2*i+1,largest,temp;
    if(l<=heapsize && a[l]>a[i])
        largest=l;
    else
        largest=i;
    if(r<=heapsize && a[r]>a[largest])
        largest=r;
    if(largest!=i)
    {
        temp=a[i];
        a[i]=a[largest];
        a[largest]=temp;
        heapify(a,largest);
    }
}

void buildheap(int a[],int length)
{
    int i;  
    heapsize=length;    
    for(i=length/2;i>=0;i--)
        heapify(a,i);
}

void heapsort(int a[],int length)
{
    int i,temp; 
    buildheap(a,length);
    for(i=length-1;i>=1;i++)
    {
        temp=a[0];
        a[0]=a[i];
        a[i]=temp;
        heapsize--;         
        heapify(a,0);   
    }
}

void main()
{
    int a[20],i,length;
    printf("ENTER THE SIZE OF YOUR ARRAY:");
    scanf("%d",&length);
    printf("ENTER THE ARRAY ELEMENTS: \n");
    for(i=0;i<length;i++)
        scanf("%d",&a[i]);
    heapsort(a,length);
    printf("THE SORTED ARRAY IS:");
    for(i=0;i<length;i++)
        printf("%d /t",a[i]);
}

注意:我使用 CLRS 中给出的堆排序算法对此进行了编码。

编辑:这是我给出的输入和我得到的错误。

chaitanya@chaitanya-laptop:~/Desktop/My prog$ ./a.out
输入阵列的大小:5
输入数组元素:
9
5
8
7
6
分段错误

编辑:显然是 i++ 而不是 i-- 的愚蠢错误导致了问题。但现在似乎存在逻辑错误,因为程序没有给出排序后的数组作为输出。

输入阵列的大小:5
输入数组元素:
2
4
3
1
9
排序后的数组为:3013077 4 1 9 2

I tried executing this code in gcc as well as turboc. In GCC it gives me a segmentation fault error at run time and in turbo it gives a null pointer assignment error again at run time.

I tried tracing it but i cant see where the problem is.

#include<stdio.h>

int heapsize;

void heapify(int a[],int i)
{
    int l=2*i,r=2*i+1,largest,temp;
    if(l<=heapsize && a[l]>a[i])
        largest=l;
    else
        largest=i;
    if(r<=heapsize && a[r]>a[largest])
        largest=r;
    if(largest!=i)
    {
        temp=a[i];
        a[i]=a[largest];
        a[largest]=temp;
        heapify(a,largest);
    }
}

void buildheap(int a[],int length)
{
    int i;  
    heapsize=length;    
    for(i=length/2;i>=0;i--)
        heapify(a,i);
}

void heapsort(int a[],int length)
{
    int i,temp; 
    buildheap(a,length);
    for(i=length-1;i>=1;i++)
    {
        temp=a[0];
        a[0]=a[i];
        a[i]=temp;
        heapsize--;         
        heapify(a,0);   
    }
}

void main()
{
    int a[20],i,length;
    printf("ENTER THE SIZE OF YOUR ARRAY:");
    scanf("%d",&length);
    printf("ENTER THE ARRAY ELEMENTS: \n");
    for(i=0;i<length;i++)
        scanf("%d",&a[i]);
    heapsort(a,length);
    printf("THE SORTED ARRAY IS:");
    for(i=0;i<length;i++)
        printf("%d /t",a[i]);
}

NOTE: I coded this using the heapsort algorithm given in CLRS.

Edit: here's the input i gave and the error I got.

chaitanya@chaitanya-laptop:~/Desktop/My prog$ ./a.out
ENTER THE SIZE OF YOUR ARRAY:5
ENTER THE ARRAY ELEMENTS:
9
5
8
7
6
Segmentation fault

Edit: apparently a silly mistake of i++ instead of i-- was causing the problem. but now there seems to be a logical error since the program is not giving the sorted array as the output.

ENTER THE SIZE OF YOUR ARRAY:5
ENTER THE ARRAY ELEMENTS:
2
4
3
1
9
THE SORTED ARRAY IS:3013077 4 1 9 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-12-24 08:06:34

下面的 for 循环永远不会终止。索引不断增加导致数组越界,这就是您收到上述错误的原因。

for(i=length-1;i>=1;i++)
{
    temp=a[0];
    a[0]=a[i];
    a[i]=temp;
    heapsize--;         
    heapify(a,0);   
}

更新:你的 heapify() 不正确..试试这个

if(l<heapsize && a[l]>a[i])
    largest=l;
else
    largest=i;
if(r<heapsize && a[r]>a[largest])
    largest=r;

The following for loop will never terminate. The index keeps on increasing causing array to go out of bounds and that's the reason you get the above error.

for(i=length-1;i>=1;i++)
{
    temp=a[0];
    a[0]=a[i];
    a[i]=temp;
    heapsize--;         
    heapify(a,0);   
}

Update : Your heapify() is incorrect.. try this instead

if(l<heapsize && a[l]>a[i])
    largest=l;
else
    largest=i;
if(r<heapsize && a[r]>a[largest])
    largest=r;
沙沙粒小 2024-12-24 08:06:34

heapsort 函数中,您应该在 for 循环中使用 i-- 而不是 i++

In the heapsort function, you should use i-- instead of i++ in the for loop.

随波逐流 2024-12-24 08:06:34
  • 如果用户输入的 length 大于 20,您肯定会溢出 a
  • heapsort 函数永远不会终止,因为 i不断增长。增长。您应该使用 i-- 来代替。
  • 最后的 printf 输出 /t,而不是制表符。将其更改为 \t

这些至少应该让您克服段错误,尽管排序看起来仍然不正确。

  • If the user inputs a length greater than 20 you most assuredly will overflow a
  • The heapsort function never terminates because i keeps growing. You should have i-- instead.
  • The printf at the end outputs /t, not a tab. Change it to \t

These should at least get you past the segfault, though the sorting still doesn't look correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文