Re: sorts that are good when a vector is largely sorted
* James Kanze:
On Apr 7, 12:29 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
Giuliano Bertoletti wrote:
Jerry Coffin ha scritto:
What facts can you give that would support the opinion that a bubble
sort is a viable alternative under any circumstance?
I like it.
I can see at a glance whether it has been implemented correctly.
That's actually the thing I like least about it. It's
invariants are more complex than those of the other O(n^2)
algoriths, so it's harder to see whether it has been implemented
correctly.
No, it's recognized as a /code pattern/.
But I don't see the problem in C++ where we have std::sort, where it's even
easier to recognize the correctness of the usage.
Anyway, I share the experience reported in this thread about only being taught
(speling?) bubble sort, and using that out of sheer ignorance.
This was at high school, I think 1980 or thereabouts. Our book just presented
bubble sort as if that was it, the only way to sort. The book, or the teacher,
explained how the code worked at the detail level, all that ingenious
"bubbling", but without any high level explanation of invariants & the like.
Thus, it happened that some other students (pupils?) tried to collect answers
from ham radio operators, I think they were called QSL cards, using the school's
radio equipment at the second floor.
And at the first floor was our computer, an 8085-based Tandberg EC-10 (I think
it was 8085, unless it was 8080), using a dialect of Basic, and with a main
screen and keyboard plus a terminal, a card reader and a thermal printer (dang,
those printouts looked bad when exposed to any temperature!).
And so I made a kind of contact database program in Basic, sorting the whole
list using bubble sort whenever a new contact was established. The guys at the
radio at second floor just phoned down the data.
This worked well for a while, but, perplexingly, it became slower and slower...
And after a few more contacts added, it became simply too slow, unable to keep
up with even the slow pace of new contacts established. :(
In a way, in modern programming as of 2009 we're blessed, because except for a
few unfortunates we don't have to care about special hardware characteristics,
like Knuth's "elevator sort" for magnetic tapes, while at that time one had to.
I think that /could/ be an explanation why the book in the late 1970's didn't
explain more about sorting: it would be an incredibly complex topic at that
time. However, nowadays I can't see any excuse for schools, colleges and
universities introducing bubble sort only. Or at all, when the goal is just to
enable students to sort -- it's not like one now has to implement sorting from
scratch, except for the unfortunates doing e.g. specialized data center things.
Cheers,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!