473,386 Members | 1,969 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,386 software developers and data experts.

Problem with heap sort

I have code which counts the number of comparison of binary ternary and quad trees what i think the code for ternary and quad trees
fixDown3 and fixDown4 are off. If you test them on some data (say run a heapSort based on them, you'll see that your positional expressions are all off).

For example, take a look at fixDown4.

template <class Item>
void fixDown4(Item a[], int k, int N)
{ int j;
while (3*k <= N)
{ c1++;
j = 4*k;
if (j < N)
{ if (a[4*k-2]>a[j]) j-=2;
if (a[4*k-1]>a[j]) j=4*k-1;
if (a[4*k+1]>a[j]) j++;
}
if (!(a[k]< a[j])) break;
exch(a[k], a[j]); k = j;
}
}

The expression 3*k <= N for testing internal nodes is wrong. Check it out on some quad-tree with the heap4 numbering.

The expression j = 4*k; won't give you the left-most brother and hence the expression j<N would cut out some nodes. The comparison in

if (a[4*k-2]>a[j]) j-=2;
if (a[4*k-1]>a[j]) j=4*k-1;
if (a[4*k+1]>a[j]) j++;

are completely off when you have only one or two brothers.
heres the code
#include <iostream>

using namespace std;

int c2=0, c3=0, c4=0;
template <class Item>
void exch(Item &A, Item &B)
{ Item t = A; A = B; B = t; }


template <class Item>
void fixDown(Item a[], int k, int N)
{ int j;
while (2*k <= N)
{ c2++;
j = 2*k;
if (j < N && a[j]< a[j+1]) j++;
if (a[k]>= a[j]) break;
exch(a[k], a[j]); k = j;
}
}

template <class Item>
void heapsort(Item a[], int l, int r)
{ int k, N = r-l+1;
for (k = N/2; k >= 1; k--)
fixDown(&a[0], k, N);
while (N > 1)
{
exch(a[1], a[N]);
fixDown(&a[0], 1, --N);
}
}

template <class Item>
void fixDown3(Item a[], int k, int N)
{ int j;
while (3*k <= N)
{ c3++;
j = 3*k;
if (j < N)
{ if (a[3*k-1]>a[j]) j--;
if (a[3*k+1]>a[j]) j++;
}
if (!(a[k]< a[j])) break;
exch(a[k], a[j]); k = j;
}
}

template <class Item>
void heapsort3(Item a[], int l, int r)
{ int k, N = r-l+1;
for (k = (N+1)/3; k >=1; k--)
fixDown3(&a[0], k, N);
while (N > 1)
{
exch(a[1], a[N]);
fixDown(&a[0], 1, --N);
}
}

template <class Item>
void fixDown4(Item a[], int k, int N)
{ int j;
while (3*k <= N)
{ c4++;
j = 4*k;
if (j < N)
{ if (a[4*k-2]>a[j]) j-=2;
if (a[4*k-1]>a[j]) j=4*k-1;
if (a[4*k+1]>a[j]) j++;
}
if (!(a[k]< a[j])) break;
exch(a[k], a[j]); k = j;
}
}

template <class Item>
void heapsort4(Item a[], int l, int r)
{ int k, N = r-l+1;
for (k = (N+1)/4; k >=1; k--)
fixDown4(&a[0], k, N);
while (N > 1)
{
exch(a[1], a[N]);
fixDown(&a[0], 1, --N);
}
}


template <class Item>
void replace_max(Item a[], Item x, int n)
{
a[1] = x;
fixDown(a, 1, n);
}

int main()
{
int m, n, i, a[100000], b[100000], c[100000];

cout << "Enter the size of the Heap: " ;
cin >> m;

for(i=1; i<=m; i++)
{ a[i] = rand();
b[i]= a[i];
c[i]= a[i];
}

heapsort(a, 1, m);
heapsort3(b, 1, m);
heapsort4(c, 1, m);

cout << "Number of comparision: "<<c2 <<" " <<c3<<" "<<c4<<"\n";

system("pause");
}
Nov 6 '08 #1
2 2268
Banfa
9,065 Expert Mod 8TB
You appear to have posted a thread from another forum, (it certainly reads as an initial post plus at least one reply). This is not acceptable behaviour on this forum.

You have also failed to describe your problem, the expected behaviour and the actual behaviour of the code, the input to the program and the outputs from it.

This is also not acceptable.

If you are going to ask a question on this forum then you need to have
  • Compiled and run your own code and made an attempt to debug it yourself.
  • Be able to describe what it is meant to do
  • Be able to describe what it does that is incorrect or undesired
  • Actually ask a question, not just post a bunch of code and expect us to run and debug it for you.
  • Put code tags round the code you post

Please read our posting guidelines particularly How to ask a question.

Further breaking of the posting guidelines may result initially in a temporary ban on your account.

Banfa
Administrator
Nov 6 '08 #2
nazguy
1
Yes, I see a couple issues with both your fixDown3 and fixDown4 routines. They are basically the same issues so I will just focus on fixDown3 and you can apply the same fixes to fixDown4.

#1 - You are right about your while condition, it is off. You can't just copy the apparent formula down from fixDown2. The node 3*k could be > N and yet you could have the node 3*k-1 = N and it would never get checked with your current code. To get the leftmost child node you would need to calculate 3*k-1 instead.

#2 - You need to rethink your logic of comparing the child nodes and exchanging. Your code is setting j to the middle node and then comparing the left node to it, decrementing j if the left node is larger. Now you have j set to the leftmost child node in this case. Then you compare the rightmost node with the node j is set to (leftmost in this case), and if the rightmost node is larger, then you increment j right back to where it started, the middle node. What you wanted was j set to the rightmost node now since it is the largest in this case. So you are exchanging the middle node instead of the rightmost node in this case.

#3 - Your condition if(j<N) will have to be rethought as well once you fix the above issues.

The implementation needs to be very precise. You can't just make assumptions when extending from binary to ternary to quad. Work out the scheme of your algorithm on paper if that helps and make sure you understand how it works precisely then implement it in code.

Hope this helps.
Nov 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: xixi | last post by:
i have a very serious memory problem, we have db2 udb v8.1 load on a HP titanium machine with 4 G memory, it is 64bit machine, currently on DB2 instance , i have three databases, but only one is...
15
by: Hemant Shah | last post by:
Folks, We have an SQL statement that was coded in an application many years ago (starting with DB V2 I think). When I upgraded to UDB 8.2, the optimizer does not use optimal path to access the...
1
by: Teemu Keiski | last post by:
Hi, I have following type of scenario (also explained here http://blogs.aspadvice.com/joteke/archive/2005/01/10/2196.aspx ) We have problematic web server (wink2 Standard, 1.5GB of physical...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
1
by: codergem | last post by:
The following code is for heap sort but its not working properly. According to my analysis the Logic is correct but still its not showing the correct output. Thanks. #include "stdafx.h"...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
11
by: Mikael Arhelger | last post by:
Hello, This has been posted a few times but still I could not find a way to connect to our database. We run DB2 Express on WIN2K server with XP clients. I can ping inside network and to the...
0
by: Raj | last post by:
We are on a db2 v8.2 (fix 8) with DPF & intra parllelism. Below are sort related configuration settings ...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
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...

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.