#include<iostream>
using namespace std;
int a,size,heapsize;
void maxheap(int a,int j)
{
int l,r,largest,temp;
l=2j;
r=(2j)+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;
}
1) Please use code tags when you post code
2) What error do you get?
I'm getting an error said as:
"Segmentation fault" after entering the array.
#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
:
#include<iostream>
using namespace std;
int a[100],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=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?
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.