required algorithm for sorting a structure

R

rushik

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.
 
C

Chris Dollin

rushik said:
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.
 
J

jacob navia

rushik said:
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.
 
K

Keith Thompson

jacob navia said:
rushik said:
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.
 
R

rushik

Chris said:
rushik said:
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.

Thanks for suggestions......I will be using qsort... my array will be
having at max 2500 structure elements.

Rushik.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top