algorithm question

  • Thread starter Thread starter matthias s.
  • Start date Start date
M

matthias s.

hi,

as a self-taught programmer I've got little clue about coding patterns and
general algorithms. so I'm asking here hoping to improve my skills:

I've got a large array of objects, say Car(s) where a Car has got a MaxSpeed
and Brand property. How would I go most effectively to search the array for
the car with a Brand equal to 'Ferrari' and the highest MaxSpeed?

It's not that I couldn't code it, but with your help, I probably could code
it cleaner/faster.

Thanks for your time!

/matthias
 
Hi

If you want some already written sorting routines, then you can get them at
my site
http://www.publicjoe.f9.co.uk/csharp/sort00.html

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html - 71
Chapters
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html - 28
Chapters
C++ Ebook at http://www.publicjoe.f9.co.uk/cppt/samples/ebook.html - 8
Chapters
Java Ebook at http://www.publicjoe.f9.co.uk/java/samples/ebook.html - 2
Chapters


Moty Michaely said:
I would try the for each approach.

int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > maxSpeed )
{
tempCar = currentCar;
}
maxSpeed = car.MaxSpeed;
}
next

tempCar should be your car.

This is the simple approach.

If you'd like, try quick sorting the array by car brand and then optimizing
your itteration. This has a potential of a complex way.

- Moty -

matthias s. said:
hi,

as a self-taught programmer I've got little clue about coding patterns and
general algorithms. so I'm asking here hoping to improve my skills:

I've got a large array of objects, say Car(s) where a Car has got a
MaxSpeed
and Brand property. How would I go most effectively to search the array
for
the car with a Brand equal to 'Ferrari' and the highest MaxSpeed?

It's not that I couldn't code it, but with your help, I probably could
code
it cleaner/faster.

Thanks for your time!

/matthias
 
I would try the for each approach.

int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > maxSpeed )
{
tempCar = currentCar;
}
maxSpeed = car.MaxSpeed;
}
next

tempCar should be your car.

This is the simple approach.

If you'd like, try quick sorting the array by car brand and then optimizing
your itteration. This has a potential of a complex way.

- Moty -
 
int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > maxSpeed )
{
tempCar = currentCar;
}
maxSpeed = car.MaxSpeed;
}
next

You might try moving maxSpeed = car.MaxSpeed;
inside the if ( currentCar.MaxSpeed > maxSpeed ).
Otherwise is plain wrong :-)
 
Back
Top