AW> I'm a student, learning C++ and as part of my homework I'm doing
AW> a small program that sorts a small array of data (only 8 elements).
AW> With an array of this size, does it really matter what sort
AW> algorithm I use? Wouldn't the dreaded Bubble sort give similar
It doesn't matter at all.
AW> Wouldn't the dreaded Bubble sort give similar
AW> performance to other algorithms, (evne if repeated 100 times)?
Yes, it would. In fact, the bubble sort is simple enough that it can
sometimes give better performance on even larger data sets over
something such as QuickSort which has a higher overhead per item. (In
my chess and checkers programs, where I usually have less than 12 moves,
(but up to 30) it's faster to just do a simple sort routine than calling
the library quick sort.)
Incidentally, the traditional bubble sort compares each and every pair
and makes the swap every time. A slightly modified version goes ahead
and makes the comparasion, but keeps track of the highest/lowest and
makes 1 swap after the comparasions are done. To sort an N element
array, you end up doing the normal N*N/2 comparasions (like bubble
sort), but at most N-1 swaps.
AW> So far I think I'll use the Selection Sort. But if there is one
AW> algorithm that shines when used on small arrays, please let me know.
There are specific, hardwired algorithms to sort small, fixed size
arrays. But they aren't very common, and usually aren't worth the
effort to add to your program. You'd likely only save a couple of
comparasions and swaps. Hardly worth the effort.
--- QScan/PCB v1.19b / 01-0162
---------------
* Origin: Jackalope Junction 501-785-5381 Ft Smith AR (1:3822/1)
|