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<iostre am>
#include<stdlib .h>
using namespace std;
int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;
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,large st,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(larges t);
}
}
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;
} 4 2103
<co******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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<iostre am> #include<stdlib .h>
using namespace std;
int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;
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,large st,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(larges t); } }
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. co******@gmail. com wrote: int _tmain(int argc, _TCHAR* argv[])
What on Earth is this?
--
Ian Collins.
"Ian Collins" <ia******@hotma il.com> wrote in message
news:4e******** *****@individua l.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.
<co******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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<iostre am> #include<stdlib .h>
using namespace std;
int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;
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,large st,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(larges t); } }
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 help but say i was totally impressed. I
needed an open source wikiblog/wikilog, whatever you wanna call it,
basically a hybrid of a blog and a wiki. I checked out snipsnap, which
uses java, it was said on their site to be a clone of vanilla, a...
|
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 the example refered to
Times New Roman - I didn't have this, but I did have Times-Roman which
worked.
My question - How do I know what fonts I *do* have available? I've run
phpinfo() and gd_info() which I hope is enough to tell some wise person...
|
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 burns.
Here is the code:
$frank = "burns";
|
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 any preconceived ideas about it. I have
noticed, however, that every programmer I talk to who's aware of Python is
also talking about Ruby. So it seems that Ruby has the potential to compete
with and displace Python. I'm curious on what basis it...
|
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 capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
| |
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 are the main criteria.
thanks, leo
|
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 sense to try to find 3D related things
that involve Python.
In fact, there seem to be quite a few--so many that I'm
not really sure what is active and growing, and what is
dying off. So I was wondering if there are Pythonistas
out there with...
|
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 concrete slurry, and others I have mercifully
forgotten about, I stumbled across pygame. (maybe not for the first
time; I think I set it aside earlier because it was described as being
aimed at a different sort of game, and besides, I had hoped wx might...
|
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 usefull. Interesting in any case.
Making decorators with this class is a snap!
|
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 to get
both some understanding of how wide candidates knowledge is, and how
much DB2 specifics they know. Of the questions below, how many do you
think are useful in determining if you've got somebody capable of
keeping a DB2 instance up,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |