Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sorting array of objects... (http://www.velocityreviews.com/forums/t592704-sorting-array-of-objects.html)

spl 02-19-2008 12:38 PM

sorting array of objects...
 
I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]
here, I have to sort obj of 10objects, based on rollno.

Please give your suggestion for any fast method?

Pedro Graca 02-19-2008 12:40 PM

Re: sorting array of objects...
 
spl wrote:
[...]
> here, I have to sort obj of 10objects, based on rollno.
>
> Please give your suggestion for any fast method?


http://en.wikipedia.org/wiki/Insertion_sort

Malcolm McLean 02-19-2008 12:41 PM

Re: sorting array of objects...
 

"spl" <splender.dev@gmail.com> wrote in message
news:e2b7068f-1035-4e16-8ba6-14b0a421bad1@p25g2000hsf.googlegroups.com...
>I have an array of objects. I want to sort the array of a
> given numeric value that exists in each of the objects. Suppose the
> array of objects is called student and the numeric value I want to
> sort the array based on the member is called "rollno".
> ex:
> struct student{
> int rollno;
> int name;
> int status;
> };
> student obj[10]
> here, I have to sort obj of 10objects, based on rollno.
>
> Please give your suggestion for any fast method?


int compfunct(const void *e1, const void *e2)
{
const student *s1 = e1;
const student *s2 = e2;

return s1->rollno - s2->rollno;
}

in main body of code:

qsort(obj, 10, sizeof(student), compfunct);

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm




Ben Bacarisse 02-19-2008 01:40 PM

Re: sorting array of objects...
 
spl <splender.dev@gmail.com> writes:

> I have an array of objects. I want to sort the array of a
> given numeric value that exists in each of the objects. Suppose the
> array of objects is called student and the numeric value I want to
> sort the array based on the member is called "rollno".


You've been pointer to qsort and this is the right advice, but...

> ex:
> struct student{
> int rollno;
> int name;
> int status;
> };
> student obj[10]


This looks like C++ not C. If youy are to use C++ (and I take it this
is homework) you might be expected to do it the C++ way. To find out
about this, post in comp.lang.c++.

[And why is the name an int?]

--
Ben.

Richard Heathfield 02-19-2008 02:00 PM

Re: sorting array of objects...
 
Ben Bacarisse said:

> spl <splender.dev@gmail.com> writes:
>

<snip>

>> struct student{
>> int rollno;
>> int name;
>> int status;
>> };
>> student obj[10]

>
> This looks like C++ not C.

<snip>
>
> [And why is the name an int?]


"I was born at 04 hundred hours on 1/3/22
Somerset House was notified
'A little 7874322 (Column 159)'.
Baptism later certified
(Parish Register Page 183):
'I name this child . . . 1242'.

Our family lived variously at 33 N.W.8
73 N.W.8, 1 and 9 N.W.11
And gloriously, for a while, in a country cottage
National Grid reference TQ 7024 (correct to one kilometre)

I passed my schooldays without much sense of identity
(Post Office Savings Book 1990 A)
In Lower and Upper 4ths and 5ths and 6ths
(Locker No 23)
Was 12th man in the 1st Eleven and rowed 7 in the Eight.

September 3rd, 1939 I was identified BIA 1526045;
1941-A. B. RNVR Red Division Starboard Watch
PJX 276123

And here I am
Stamped sealed and delivered

Passport No. 77922 F.O. London 23/2/66
National Insurance ZB 88 81 17B
Driving Licence 5Z/ 107206
Telephone Number 01 -983- Double 747
To my doctor: BIA 1526045
To my Banker: 37705873
To my Union: B 16776

V.A.T. 223 3859 66 . . .

And TIME LIFE International (Amsterdam) Inc. addresses me as
Mr Flanders 581 101 L03 FLAN-063 M 992 but refers to me in
private as 400000008400 1 00183N 06 S 29.

My days are numbered."

---- Michael Flanders (1/3/1922 - 14/4/1975)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

CBFalconer 02-19-2008 11:33 PM

Re: sorting array of objects...
 
spl wrote:
>
> I have an array of objects. I want to sort the array of a
> given numeric value that exists in each of the objects. Suppose the
> array of objects is called student and the numeric value I want to
> sort the array based on the member is called "rollno".
> ex:
> struct student{
> int rollno;
> int name;
> int status;
> };
> student obj[10]
> here, I have to sort obj of 10objects, based on rollno.


To start with, there is not such type as 'student'. There is a
type 'struct student'. Unless you are using C++, in which case
this is the wrong newsgroup.

Your first project is to decide whether one object is greater,
equal, or less than another, based on the decisions you outlined.
So will need a routine like the following:

int cmpstudent(const void *leftstu, const void *rightstu) {
const struct student *left = leftstu;
const struct student *right = rightstu;

return (leftstu->rollno > rightstu->rollno)
-(leftstu->rollno < rightstu->rollno);
}

The use of void* allows use with such routines as qsort etc. Note
the absence of casts. Casts are usually errors. Note that
cmpstudent needs to know what it is comparing, but the code that
passes the void* pointers doesn't.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com


pete 02-19-2008 11:34 PM

Re: sorting array of objects...
 
Malcolm McLean wrote:
>
> "spl" <splender.dev@gmail.com> wrote in message
> news:e2b7068f-1035-4e16-8ba6-14b0a421bad1@p25g2000hsf.googlegroups.com...
> >I have an array of objects. I want to sort the array of a
> > given numeric value that exists in each of the objects. Suppose the
> > array of objects is called student and the numeric value I want to
> > sort the array based on the member is called "rollno".
> > ex:
> > struct student{
> > int rollno;
> > int name;
> > int status;
> > };
> > student obj[10]
> > here, I have to sort obj of 10objects, based on rollno.
> >
> > Please give your suggestion for any fast method?

>
> int compfunct(const void *e1, const void *e2)
> {
> const student *s1 = e1;
> const student *s2 = e2;
>
> return s1->rollno - s2->rollno;
> }
>
> in main body of code:
>
> qsort(obj, 10, sizeof(student), compfunct);


That's undefined if s1->rollno equals INT_MAX and s2->rollno equals -1,
and what you wrote
is also undefined for about a zillion other scenarios.

int compfunct(const void *e1, const void *e2)
{
const student *s1 = e1;
const student *s2 = e2;

return s2->rollno > s1->rollno ? -1 : s2->rollno != s1->rollno;
}

--
pete


All times are GMT. The time now is 05:36 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.