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

heap sort

Hi all,

Is there a simple algorithm to implement in heapsort???
this is the program i found in my college D.S notebook
#include<stdio.h>
#include<stdlib.h>

void heapsort (int[], int);
void siftdown (int[], int, int);

int
main (void)
{

int a[5] = { 44, 22, 66, 77, 11 };
int i;

puts ("\n Before Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

heapsort (a, 5);

puts ("\n After Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

puts ("");
return (EXIT_SUCCESS);
}

void
heapsort (int a[5], int size)
{
int i, temp;

/*
* heap creation
*/

for (i = size / 2; i >= 0; i--)
{
siftdown (a, i, size - 1);
}

for (i = (size - 1); i >= 1; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
siftdown (a, 0, i - 1);
} /*finding the max element & putting in last pos
*/

}

void
siftdown (int a[5], int r, int b)
{
int done, maxchild, temp;
done = 0;

while ((r * 2 <= b) && (!done))
{
if (r * 2 == b)
maxchild = r * 2;
else if (a[r * 2] a[r * 2 + 1])
maxchild = r * 2;
else
maxchild = r * 2 + 1;

/*check if root element < max child element,if it so swap,
* set root pos = max child pos
* */

if (a[r] < a[maxchild])
{
temp = a[r];
a[r] = a[maxchild];
a[maxchild] = temp;
r = maxchild;
}
else
done = 1;
}

}

how good is this program??? is there a better/simpler algorithm to
create heap than which is used here...???

Nov 13 '07 #1
9 8041
On Tue, 13 Nov 2007 14:30:03 -0000, so**********@gmail.com wrote:
>Is there a simple algorithm to implement in heapsort???
this is the program i found in my college D.S notebook
http://en.wikipedia.org/wiki/Sorting_algorithm (particularly
http://en.wikipedia.org/wiki/Sorting...ing_algorithms)

then

http://en.wikipedia.org/wiki/Heapsort
--
PGP key ID 0xEB7180EC
Nov 13 '07 #2
On Nov 13, 6:30 am, sophia.ag...@gmail.com wrote:
Hi all,

Is there a simple algorithm to implement in heapsort???
this is the program i found in my college D.S notebook

#include<stdio.h>
#include<stdlib.h>

void heapsort (int[], int);
void siftdown (int[], int, int);

int
main (void)
{

int a[5] = { 44, 22, 66, 77, 11 };
int i;

puts ("\n Before Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

heapsort (a, 5);

puts ("\n After Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

puts ("");
return (EXIT_SUCCESS);

}

void
heapsort (int a[5], int size)
{
int i, temp;

/*
* heap creation
*/

for (i = size / 2; i >= 0; i--)
{
siftdown (a, i, size - 1);
}

for (i = (size - 1); i >= 1; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
siftdown (a, 0, i - 1);
} /*finding the max element & putting in last pos
*/

}

void
siftdown (int a[5], int r, int b)
{
int done, maxchild, temp;
done = 0;

while ((r * 2 <= b) && (!done))
{
if (r * 2 == b)
maxchild = r * 2;
else if (a[r * 2] a[r * 2 + 1])
maxchild = r * 2;
else
maxchild = r * 2 + 1;

/*check if root element < max child element,if it so swap,
* set root pos = max child pos
* */

if (a[r] < a[maxchild])
{
temp = a[r];
a[r] = a[maxchild];
a[maxchild] = temp;
r = maxchild;
}
else
done = 1;
}

}

how good is this program??? is there a better/simpler algorithm to
create heap than which is used here...???
Your post is topical in news:comp.programming -- which talks about
various aspects of programming including algorithms.

That code looks like it came from here:
http://linux.wku.edu/~lamonml/algor/sort/heap.html
and specifically:
http://linux.wku.edu/~lamonml/algor/sort/heap.c
but the code on the web site is a bit better than the book's. I hope
the book gave credit.

The sort looks well executed to me, but I have not bothered to test or
benchmark it. Generally speaking, heapsort is used as a "last resort"
in the introspective sort algorithm.

Follow-up set to news:comp.programming

Nov 13 '07 #3
so**********@gmail.com wrote:
>
Hi all,

Is there a simple algorithm to implement in heapsort???
this is the program i found in my college D.S notebook

#include<stdio.h>
#include<stdlib.h>

void heapsort (int[], int);
void siftdown (int[], int, int);

int
main (void)
{

int a[5] = { 44, 22, 66, 77, 11 };
int i;

puts ("\n Before Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

heapsort (a, 5);

puts ("\n After Sorting:: ");
for (i = 0; i < 5; i++)
printf ("%d\t", a[i]);

puts ("");
return (EXIT_SUCCESS);
}

void
heapsort (int a[5], int size)
{
int i, temp;

/*
* heap creation
*/

for (i = size / 2; i >= 0; i--)
{
siftdown (a, i, size - 1);
}

for (i = (size - 1); i >= 1; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
siftdown (a, 0, i - 1);
} /*finding the max element & putting in last pos
*/

}

void
siftdown (int a[5], int r, int b)
{
int done, maxchild, temp;
done = 0;

while ((r * 2 <= b) && (!done))
{
if (r * 2 == b)
maxchild = r * 2;
else if (a[r * 2] a[r * 2 + 1])
maxchild = r * 2;
else
maxchild = r * 2 + 1;

/*check if root element < max child element,if it so swap,
* set root pos = max child pos
* */

if (a[r] < a[maxchild])
{
temp = a[r];
a[r] = a[maxchild];
a[maxchild] = temp;
r = maxchild;
}
else
done = 1;
}

}

how good is this program??? is there a better/simpler algorithm to
create heap than which is used here...???
These functions:
void hsortm(e_type *array, size_t nmemb);
void hsortx1(e_type *array, size_t nmemb);
void heapSort2(e_type *array, size_t nmemb);
void heapSort2p1(e_type *array, size_t nmemb);
void husort(e_type *array, size_t nmemb);
void husort2(e_type *array, size_t nmemb);
void hesort(e_type *array, size_t nmemb);
at
http://www.mindspring.com/~pfilandr/C/e_driver/e_sort.c
are different implementations of heapsort.

I don't think that any one of them
is simpler than what you've posted.

--
pete
Nov 14 '07 #4
[comp.lang.c] Philip Potter <pg*@see.sig.invalidwrote:
Same again here. Declare 'int a[]' or 'int *a'.
I personally prefer never to use 'type foo[]' - the extra character
really doesn't buy much except some confusion for the unwary.
It looks fine to me. [WRT some heapsort code]
The Java programmer in me (the me that pays the bills these days) says
"Write some unit tests" (to the OP of course).

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Nov 15 '07 #5

pete wrote:
I use lower case for the str() and xstr() macros,
because I also copy them out of the standard that way.
str() macro in the standard?
you mean the dummy example for macro usage (6.10.3.5)?

is it useful?
Nov 15 '07 #6
Szabolcs Nagy wrote:
>
pete wrote:
I use lower case for the str() and xstr() macros,
because I also copy them out of the standard that way.

str() macro in the standard?
you mean the dummy example for macro usage (6.10.3.5)?
Yes.
is it useful?
It can be.

#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)

char array[LENGTH + 1];

int rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);

--
pete
Nov 15 '07 #7
"pete" <pf*****@mindspring.coma écrit dans le message de news:
47***********@mindspring.com...
Szabolcs Nagy wrote:
>>
pete wrote:
I use lower case for the str() and xstr() macros,
because I also copy them out of the standard that way.

str() macro in the standard?
you mean the dummy example for macro usage (6.10.3.5)?

Yes.
>is it useful?

It can be.

#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)

char array[LENGTH + 1];

int rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
Thanks for pointing out how limited fscanf is. Improvements are badly
needed such as the ability to pass dynamic sizes for destination arrays.
The macro trick above is a limited solution, contorted, partial and fragile:
a good example of stringization, but also a good example of cryptic code to
be avoided.

--
Chqrlie.
Nov 25 '07 #8
Charlie Gordon wrote:
"pete" <pf*****@mindspring.coma écrit dans le message de news:
47***********@mindspring.com...
>Szabolcs Nagy wrote:
>>pete wrote:
I use lower case for the str() and xstr() macros,
because I also copy them out of the standard that way.
str() macro in the standard?
you mean the dummy example for macro usage (6.10.3.5)?
Yes.
>>is it useful?
It can be.

#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)

char array[LENGTH + 1];

int rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);

Thanks for pointing out how limited fscanf is. Improvements are badly
needed such as the ability to pass dynamic sizes for destination arrays.
The macro trick above is a limited solution, contorted, partial and fragile:
a good example of stringization, but also a good example of cryptic code to
be avoided.
Interesting. A couple of observations.

First, this particular method can be used only if you have the length as
a literal decimal integer constant. Stringizing something like
``sizeof(buf)'' won't do you much good here. A more general solution is
to use sprintf() or snprintf() to build the format string; this requires
no precessor tricks, but it imposes some runtime overhead, and
determining the required length for the format string can be tricky.

Second, fprintf allows a precision or length modifier to be specified
with an asterisk '*', which causes the value to be read from an int
argument. I wonder why fscanf doesn't have this facility. fscanf uses
'*' to denote assignment suppression, but surely an alternative syntax
could have been invented.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 27 '07 #9
In article <fi**********@aioe.orgKeith Thompson <ks***@mib.orgwrote:
[much snippage]
>Second, fprintf allows a precision or length modifier to be specified
with an asterisk '*', which causes the value to be read from an int
argument. I wonder why fscanf doesn't have this facility. fscanf uses
'*' to denote assignment suppression, but surely an alternative syntax
could have been invented.
Indeed -- yet I would suggest that adding indirect field widths to
scanf is a little like adding a coffee-pot heater to a chainsaw
that has no off-switch ("this thing wastes power, we could use it
to heat our coffee when we're not cutting trees"). Sure, it would
work fine, but it would be better to use a device with safer modes
of operation (i.e., to replace the scanf family with something that
"works right" :-) ).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 30 '07 #10

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

Similar topics

0
by: Stefan Behnel | last post by:
Hi! I filed a patch for a Heap class to be integrated into the heapq module (in addition the the currently available functions). The main features are support for iteration and for the standard...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
3
by: darth | last post by:
Does anyone have heap sorting with queue(doublely linked list) in C? I have heap sort with array but with queue, it's somewhat hard.
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"...
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: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...

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.