C
codergem
Helo friends
Could any of you please help me out with this problem.
To me this program of heap sort seems logically perfect but still the
output of this is not coming right,
I dont want to follow any other solution.Could any of you please take
out some precious time of yours in solving this big problem for me,
where m i going wrong???
Thanks in advance.
// heapsort111.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
int n=10,s[10]={11,2,9,13,57,25,17,1,90,3},heapsize;
void swap(int a,int b)
{
int temp=s;
s=s[a];
s[a]=temp;
}
void keepheap(int i)
{
int l,r,p,q,t,largest,m;
l = 2*i;
r = 2*i + 1;
p = s[l];
q = s[r];
t = s;
if(l<heapsize && p > t)
largest = l;
else
largest = i;
m = s[largest];
if(r<heapsize && q > m)
largest = r;
if(largest != i)
{
swap(i, largest);
keepheap(largest);
}
}
void makeheap(int n)
{
heapsize=n;
for(int i=(n/2)-1; i>=0; i--)
keepheap(i);
}
void heapsort()
{
makeheap(n);
for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++)
cout<<s<<"; ";
heapsort();
cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++)
cout<<s<<"; ";
return 0;
}
Could any of you please help me out with this problem.
To me this program of heap sort seems logically perfect but still the
output of this is not coming right,
I dont want to follow any other solution.Could any of you please take
out some precious time of yours in solving this big problem for me,
where m i going wrong???
Thanks in advance.
// heapsort111.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
int n=10,s[10]={11,2,9,13,57,25,17,1,90,3},heapsize;
void swap(int a,int b)
{
int temp=s;
s=s[a];
s[a]=temp;
}
void keepheap(int i)
{
int l,r,p,q,t,largest,m;
l = 2*i;
r = 2*i + 1;
p = s[l];
q = s[r];
t = s;
if(l<heapsize && p > t)
largest = l;
else
largest = i;
m = s[largest];
if(r<heapsize && q > m)
largest = r;
if(largest != i)
{
swap(i, largest);
keepheap(largest);
}
}
void makeheap(int n)
{
heapsize=n;
for(int i=(n/2)-1; i>=0; i--)
keepheap(i);
}
void heapsort()
{
makeheap(n);
for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++)
cout<<s<<"; ";
heapsort();
cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++)
cout<<s<<"; ";
return 0;
}