473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

What is the problem with this???

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[b];
s[b]=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[i];

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[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++)
cout<<s[i]<<"; ";

return 0;
}

Jun 1 '06 #1
4 2076

<co******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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[b];
s[b]=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[i];

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[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++)
cout<<s[i]<<"; ";

return 0;
}


Someone posts code and says "To me this program of heap sort seems logically
perfect but still the output of this is not coming right" I'll glance at the
code for obvious errors and go on may way.

If you want quick answers, show us what you expect the output to be, and
what the actual output is.
Jun 2 '06 #2
co******@gmail.com wrote:
int _tmain(int argc, _TCHAR* argv[])


What on Earth is this?

--
Ian Collins.
Jun 2 '06 #3

"Ian Collins" <ia******@hotmail.com> wrote in message
news:4e*************@individual.net...
co******@gmail.com wrote:
int _tmain(int argc, _TCHAR* argv[])


What on Earth is this?


Microsoft junk.

He simply needs to not use precompiled headers.
Jun 2 '06 #4

<co******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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,
We don't know the "logic" of the program, since you haven't specified it in
comments anywhere.

Regarding the output: tell us what the output is, and what you expect it to
be, not just that it "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;
Why are you declaring n as a global variable here? Why not just pass 10 to
the function heapsort()? You're likely to have troubles (at least using
your debugger) with a local variable named n and a global variable also
named n. At the least, you might forget which one you're referring to at
any given time.

void swap(int a,int b)
{
int temp=s[b];
s[b]=s[a];
s[a]=temp;
}

Comments as to what each function is supposed to do would really help here.
(And they should be part of your design process, before you actually add
code to the functions, not an afterthought.)
void keepheap(int i)
{
int l,r,p,q,t,largest,m;
Ok, "largest" is a pretty good name, but the rest of those are meaningless,
and won't help you (or anyone else) figure out what they're used for.

l = 2*i;
r = 2*i + 1;

p = s[l];
q = s[r];
t = s[i];

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);
You only call makeheap once, from here, and pass it the global variable n.
Why have a separate function makeheap at all? Why not just put the loop
from there in this function? I think that _might_ help you see what's going
on better in this case, (especially in relation to the variables n and
heapsize and the loops you're executing).

(I know it's good to break down code into single tasks, but you're not being
consistent when you put the first loop into makeheap, but keep the second
loop in this function.)

for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
You should consider not using precompiled headers, removing the include of
stdafx.h above, and using a standard main function here.
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++)
cout<<s[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++)
cout<<s[i]<<"; ";

return 0;
}


Use a debugger, and step through the code. See if it's doing what you
expect. Write down what you _expect_ to see at each step on paper, then
copmare against that. At some point, what you see in the debugger won't
match what you expected. That should point out the error in your code.

-Howard


Jun 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
8
by: Randell D. | last post by:
I have just recompiled, upgraded to PHP 4.3.4. As an exercise (and curiosity) I've decided to test out PDF functions and got the test in the PHP online manual working. I had one problem whereby...
2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
11
by: Leo | last post by:
hi there for somebody who wants tostart small/medium GUI apps with python: what's the best toolkit: tkinter, wxPython or what? stability, ease of use and portability between mac and windows...
3
by: Kenneth McDonald | last post by:
One of my current "fun" goals is to start playing with 3D related things, purely because I find it of interest. Since I also use and love Python in more serious endeavours, it would seem to make...
9
by: Martin Maney | last post by:
In my copious spare time I've been dabbling at getting a computerized version of a board game working. After deciding that tk just made me want to vomit, and wx was like swimming through...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
30
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.