I have been using what I thought was a bubble sort
Francis Siu
SCJP, MCDBA
Originally posted by Flo Powers:
However, does not my version above actually perform worse than the selection sort, in that it not only compares a[i] with all the later elements, but also swaps with every single element after it in the array that contains a lower value? I.e. there is unnecessary moving of data, as the algorithm doesn't wait until it has identified the lowest value in the rest of the elements of the array. Instead, the inner for-loop swaps with every element that is lower, until it has gone through the rest of the array.
If the data is already mostly sorted, quicksort is exceedingly slow. a bubble sort can be exceedingly fast in such cases.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|