473,228 Members | 1,796 Online

# 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 2134
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 thread has been closed and replies have been disabled. Please start a new discussion.