473,385 Members | 1,673 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,385 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 2082

<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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.