views:

172

answers:

4

The C++ book I'm reading described a sort algo, saying it is the Bubblesort yet I cannot find a single variation of bubblesort just like it. I understand the differences are minor, but is it exactly as efficient as a regular bubblesort ?

BubbleSort(int A[], int length)
for (j=0; j < length-1; j++)
  for (i=j+1; i < length; i++)
    if (A[i] < A[j])
      Swap()

Basically, instead of comparing two adjacent values, it compares the first A[0] with every entry, on the next pass it compares A[1] with the remaining entries, then A[2] etc.

Is it really just a regular bubblesort, is the characteristics and performance exactly the same?

+2  A: 

This is selection sort. On each pass you find the i'th smallest element and put it in position i. After the first pass, it is not necessary to look at A[0] anymore, and so on. Selection sort is worst-case O(n2) and best-case O(n), just like bubble sort, but it has a smaller constant factor than bubble sort. Insertion sort, an additional refinement, is even better, to the point where it's faster than most O(n log n) algorithms for very small arrays (fewer than ten elements or so) and so serious library sort primitives will cut over to it for small subproblems.

Zack
It is similar to selection sort yes, but it differs by swapping A[0] with every element it finds that is smaller. So in say a reverse ordered array with N elements, it would perform N-1 swaps, whereas a regular selection sort would only perform 1 swap, since it finds the correct (lowest) element before swapping.
eZet
Sorry, you're correct. It is exactly like a selection sort, except for the block within the IF which I didn't type out fully in the code example. In the book it actually uses the same IF body as a Bubblesort does, leaving out the part where it first finds the minimum value. So the author has taken the selection sort, removed the "efficient" parts of it and called it a bubblesort. Great. :)
eZet
A: 

The algorithm looks more like insert sort to me, as they share the same (outer) loop invariant -- after j-th iteration of the outer loop, the j smallest elements are in their correct positions.

On the other hand, the characteristic property of bubble sort is that it always swaps neighboring elements (a property which is not satisfied in your snippet).

The time complexity of this algorithm is O(n^2), just like bubble sort and insert sort (and quick sort in the worst case).

avakar
You're right, this isn't a refinement of bubble sort at all. I think it's actually selection sort (see my edits)
Zack
@Zack, you're absolutely right, I got the names mixed up (the invariant is that of a select sort).
avakar
A: 

It looks like a Selection Sort to me.

Adrian McCarthy
+1  A: 

This sort is similar to selection sort, in that each pass through the outer loop identifies the best element and removes it from further consideration. In a traditional selection sort, however, the best element is swapped with the element to be removed and other elements are left alone. In the sort you list (found, IIRC, in "A Basic Approach to Basic" among other places) some other elements will get swapped around as well. I don't think the extra swaps accomplish anything particularly useful, and the sort loses out on just about the only advantage bubble sort has (suitability for implementation on purely-sequential-access media)

supercat
So basically it's just a bad combination of bubblesort and selection sort? I found it in "Object-Oriented Programming in C++" by the way.
eZet
@eZet: Actually it's an "unimproved" selection sort. The basic selection sort (as defined) acts exactly as above. I have a [visual comparison of both](http://goo.gl/tZni) available. The "improved" version stores the minimum value and once it reaches the end swaps it.
Josh K
@Josh K: Thanks, that explains alot.
eZet