Richard said:
Give a programming newbie a computer, however, and tell them to
write a sort routine, and they may well end up with bubble sort as
their first attempt.
I wouldn't expect that to be the common result unless the newbie had
already heard of bubble-sort.
Ditto.
The earliest program I can remember writing implemented an algorithm
which makes even bubble sort look good by comparison. It was written
in Fortran I, and I certainly don't have the text of that program any
more, but here's the same algorithm implemented in C99:
void sort_int(int array[], int n)
{
// I am quite certain my Fortran I version
// had nothing like the following statement:
if(array==NULL || n<2)
return;
for(int start=0; start < n - 1; start++)
{
int min = array[start];
int location = start;
// Find smallest element in rest of list
for(int next = start+1; next < n; next++)
if(array[next] < min)
{
min = array[next];
location = next;
}
if(location != start)
{ // Swap elements
int temp = array[start];
array[start] = array[location];
array[location] = temp;
}
}
}
That is an algorithm I could imagine a newbie inventing, but oddly
enough, that's not what actually happened - that's the algorithm our
teacher instructed us to implement.
More than three decades after the fact, I can't be sure, but I think
that if I'd been asked to choose my own sorting algorithm, I might
have come up with something like an insertion sort.