469,576 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

required algorithm for sorting a structure

Hello all,

I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.

structure is

struct data
{
int account;
int balance;
};

struct data d[200];

I want to sort an array of this structure based on 'balance' (array can
have multiple entries for same account number), my array size will be
around 150-200 elements.

Can anyone suggest me some optimized sorting algorithm for same.

Thanks,
Rushik.

Oct 3 '06 #1
4 1939
rushik wrote:
Hello all,

I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.

structure is

struct data
{
int account;
int balance;
};

struct data d[200];

I want to sort an array of this structure based on 'balance' (array can
have multiple entries for same account number), my array size will be
around 150-200 elements.

Can anyone suggest me some optimized sorting algorithm for same.

Thanks,
Rushik.
Use qsort, and write a comparison function that returns -1, 0 or 1
depending on the balance comparison.
Oct 3 '06 #2
rushik wrote:
Hello all,

I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.

structure is

struct data
{
int account;
int balance;
};

struct data d[200];

I want to sort an array of this structure based on 'balance' (array can
have multiple entries for same account number), my array size will be
around 150-200 elements.

Can anyone suggest me some optimized sorting algorithm for same.
For a mere 200 elements, why do you want an "optimised" sorting
algorithm?

Use `qsort` unless you have a reason not to.

--
Chris "falling further in" Dollin
"It took a very long time, much longer than the most generous estimates."
-James White, /Sector General/

Oct 3 '06 #3
jacob navia <ja***@jacob.remcomp.frwrites:
rushik wrote:
>I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.
structure is
struct data
{
int account;
int balance;
};
struct data d[200];
I want to sort an array of this structure based on 'balance' (array
can
have multiple entries for same account number), my array size will be
around 150-200 elements.
Can anyone suggest me some optimized sorting algorithm for same.

Use qsort, and write a comparison function that returns -1, 0 or 1
depending on the balance comparison.
qsort() does impose some overhead, since it has to perform an indirect
function call for each comparison. You might get better performance
with a sorting routine that's customized for your type and the kind of
comparison you want to do.

*But* it makes little sense to do this unless you've tried using
qsort(), *measured* its performance, and confirmed that it's a
bottleneck.

If qsort() really isn't fast enough, you could probably adapt an
existing open-source implementation of qsort() to your purposes,
replacing the indirect calls to the comparison function with direct
comparisons.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 3 '06 #4

Chris Dollin wrote:
rushik wrote:
Hello all,

I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.

structure is

struct data
{
int account;
int balance;
};

struct data d[200];

I want to sort an array of this structure based on 'balance' (array can
have multiple entries for same account number), my array size will be
around 150-200 elements.

Can anyone suggest me some optimized sorting algorithm for same.

For a mere 200 elements, why do you want an "optimised" sorting
algorithm?

Use `qsort` unless you have a reason not to.

--
Chris "falling further in" Dollin
"It took a very long time, much longer than the most generous estimates."
-James White, /Sector General/
Thanks for suggestions......I will be using qsort... my array will be
having at max 2500 structure elements.

Rushik.

Oct 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Shaunak Kashyap | last post: by
16 posts views Thread by cody | last post: by
11 posts views Thread by rkbnair | last post: by
5 posts views Thread by chrisguest | last post: by
51 posts views Thread by Joerg Schoen | last post: by
reply views Thread by JosAH | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.