[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Low-level vs. High-level programming (was My First Computer)
In article <n%mIa.575$aR6.17@newssvr19.news.prodigy.com>,
Kelly Hall <hall@priest.com> wrote:
> "Paul Schlyter" <pausch@saaf.se> wrote in message
> news:bcsp35$ov7$1@merope.saaf.se...
>> Of course not! So we can agree bubblesort is here an "extremely
>> bad algorithm". And one wouldn't need to be a brilliant Java
>> programmer to write a quicksort which outperformed a bubblesort
>> written in C.
>
> Bubblesort is a lousy general-purpose sorting algorithm. But in the
> rare case where either you know your data is mostly sorted anyway,
In that case, quicksort can be a REALLY bad algorithm.... :-)
One way to take care of that situation is to run just one round
of bubble-sort, in one direction, and then run another one, in
the other direction. Now, if the original data was almost sorted,
it should now be completely sorted; to check for that, run a third
round of bubble-sort, in either direction and count the number of
swaps; if no swaps are performed, the data is sorted. If the data
isn't sorted after this, switch to e.g. quicksort.
> or the data set is quite small, then the bubble sort is a good
> choice.
I can think of a third case when bubble-sort can be a good choice: if
your available code space is severely limited (in e.g. a small
empedded processor where the ROM already is almost full with other
code needed for the project) and if the increased execution time is
acceptable, bubble sort may be the best choice since it's such a
simple algorithm which consumes little code.
> Modified bubble sorts are often used as a special-case routines
> in industrial-strength sorting libraries.
>
> As I've said before, have a big toolbox and use the right tool
> for the job :)
Indeed true.... :-)
--
----------------------------------------------------------------
Paul Schlyter, Grev Turegatan 40, SE-114 38 Stockholm, SWEDEN
e-mail: pausch at stockholm dot bostream dot se
WWW: http://www.stjarnhimlen.se/
http://home.tiscali.se/pausch/