# Help... runtime error in my maxheap sort program

**URL:** <https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848>\
**Category:** Programming\
**Created:** [April 6, 2011, 6:26am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848 "2011-04-06T06:26:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![poonam.gaigole](https://community.unix.com/user_avatar/community.unix.com/poonam.gaigole/32/2122_2.png) [@poonam.gaigole](https://community.unix.com/u/poonam.gaigole)\
**Post date:** [April 6, 2011, 6:26am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/1 "2011-04-06T06:26:29Z")

</div>

```nohighlight

```

#include\<iostream\>  
using namespace std;  
int _a,size,heapsize;  
void maxheap(int a,int j)  
{  
int l,r,largest,temp;  
l=2j;  
r=(2_j)+1;  
for(j=0;j\<size;++j)  
{  
if(l\<=heapsize && a[l]\>a[j])  
largest=l;  
else  
largest=j;  
if(r\<=heapsize && a[r]\>a[largest])  
largest=r;  
if(largest!=j)  
{  
a[temp]=a[j];  
a[j]=a[largest];  
a[largest]=a[temp];  
maxheap(a,largest);  
}  
}  
}  
void buildmax(int \*a)  
{  
heapsize=size;  
int n=size/2;  
for(int i=n;i\>0;--i)  
maxheap(a,i);  
}  
void heapsort(int \*a)  
{  
int tempo;  
buildmax(a);  
for(int i=heapsize;i\>0;--i)  
{  
a[tempo]=a[0];  
a[0]=a[i];  
a[i]=a[tempo];  
--heapsize;  
maxheap(a,0);  
}  
}  
int main()  
{  
cout\<\<"\nEnter the number of elements you want to enter in the array: ";  
cin\>\>size;  
int _a= new int [size];  
cout\<\<"\nEnter an array of elements: ";  
for(int i=0;i\<size;++i)  
cin\>\>a[i];  
heapsort(a);  
/_ cout\<\<"\nThe corresponding array is: ";  
for(int i=0;i\<size;++i)  
cout\<\<a[i]; \*/  
buildmax(a);  
cout\<\<"\nThe sorted array is: ";  
for(int i=0;i\<size;++i)  
cout\<\<a[i]\<\<"\t";  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![JohnGraham](https://community.unix.com/letter_avatar/johngraham/32/6_22734026db12803ebb3e059622ac3534.png) [@JohnGraham](https://community.unix.com/u/JohnGraham)\
**Post date:** [April 6, 2011, 6:49am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/2 "2011-04-06T06:49:31Z")

</div>

1) Please use code tags when you post code  
2) What error do you get?

---

<div class="post-metadata">

**Author:** ![poonam.gaigole](https://community.unix.com/user_avatar/community.unix.com/poonam.gaigole/32/2122_2.png) [@poonam.gaigole](https://community.unix.com/u/poonam.gaigole)\
**Post date:** [April 6, 2011, 6:52am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/3 "2011-04-06T06:52:51Z")

</div>

I'm getting an error said as:  
"Segmentation fault" after entering the array.

---

<div class="post-metadata">

**Author:** ![disaster](https://community.unix.com/letter_avatar/disaster/32/6_22734026db12803ebb3e059622ac3534.png) [@disaster](https://community.unix.com/u/disaster)\
**Post date:** [April 6, 2011, 7:05am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/4 "2011-04-06T07:05:05Z")

</div>

```nohighlight
#include<iostream>
using namespace std;
int *a,size,heapsize;
void maxheap(int *a,int j)
{
    int l,r,largest,temp;
    l=2*j;
    r=(2*j)+1;
    for(j=0;j<size;++j)
    {
        if(l<=heapsize && a[l]>a[j])
            largest=l;
        else
            largest=j;
        if(r<=heapsize && a[r]>a[largest])
            largest=r;
        if(largest!=j)
        {
            a[temp]=a[j];
            a[j]=a[largest];
            a[largest]=a[temp];
            maxheap(a,largest);
        }
    }    
}
void buildmax(int *a)
{
    heapsize=size;
    int n=size/2;
    for(int i=n;i>0;--i)
        maxheap(a,i);
}
void heapsort(int *a)
{
    int tempo;
    buildmax(a);
    for(int i=heapsize;i>0;--i)
    {
        a[tempo]=a[0];  
        a[0]=a;
        a=a[tempo];
        --heapsize;
        maxheap(a,0);
    }
}
int main()
{
    cout<<"\nEnter the number of elements you want to enter in the array: ";
    cin>>size;
    int *a= new int ;
    cout<<"\nEnter an array of elements: ";
    for(int i=0;i<size;++i)
        cin>>a;
    heapsort(a);
   /* cout<<"\nThe corresponding array is: ";
    for(int i=0;i<size;++i)
        cout<<a; */
    buildmax(a);
    cout<<"\nThe sorted array is: ";
    for(int i=0;i<size;++i)
        cout<<a<<"\t";
    return 0;
}

```

What do you consider "entering the array".  
Anyways, in function heapsort you make use of an uninitialized int tempo, so I assume you will most likely have the error there

---

<div class="post-metadata">

**Author:** ![poonam.gaigole](https://community.unix.com/user_avatar/community.unix.com/poonam.gaigole/32/2122_2.png) [@poonam.gaigole](https://community.unix.com/u/poonam.gaigole)\
**Post date:** [April 6, 2011, 9:27am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/5 "2011-04-06T09:27:07Z")

</div>

```nohighlight

```

:  
#include\<iostream\>  
using namespace std;  
int a[100],size,heapsize;  
void maxheap(int _a,int j)  
{  
int l,r,largest,temp;  
l=2_j;  
r=(2\*j)+1;  
for(j=0;j\<size;++j)  
{  
if(l\<=heapsize && a[l]\>a[j])  
largest=l;  
else  
largest=j;  
if(r\<=heapsize && a[r]\>a[largest])  
largest=r;  
if(largest!=j)  
{  
a[temp]=a[j];  
a[j]=a[largest];  
a[largest]=a[temp];  
maxheap(a,largest);  
}  
}  
}  
void buildmax(int \*a)  
{  
heapsize=size;  
int n=size/2;  
for(int i=heapsize;i\<n;--i)  
maxheap(a,i);  
}  
void heapsort(int \*a)  
{  
int tempo;  
buildmax(a);  
for(int i=size;i\>0;--i)  
{  
tempo=a[0];  
a[0]=a[i];  
a[i]=tempo;  
--heapsize;  
maxheap(a,0);  
}  
}  
int main()  
{  
cout\<\<"\nEnter the number of elements you want to enter in the array: ";  
cin\>\>size;  
cout\<\<"\nEnter an array of elements: ";  
for(int i=0;i\<size;++i)  
cin\>\>a[i];  
heapsort((int \*)a[size]);  
cout\<\<"\nThe corresponding array is: ";  
for(int i=0;i\<size;++i)  
cout\<\<a[i];  
buildmax((int \*)a[size]);  
cout\<\<"\nThe sorted array is: ";  
for(int i=0;i\<size;++i)  
cout\<\<a[i];  
return 0;  
}

---------- Post updated at 06:57 PM ---------- Previous update was at 06:50 PM ----------

Why is my code tags not working when I'm trying to post a code?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [April 6, 2011, 10:00am UTC](https://community.unix.com/t/help-runtime-error-in-my-maxheap-sort-program/285848/6 "2011-04-06T10:00:41Z")

</div>

> [@](#):
>
> Why is my code tags not working when I'm trying to post a code?

Quote your own post and you'll see what you did wrong. Your code tags aren't around your code, they're just kind of sitting somewhere by themselves.
