name of a sorting algorithm

Ian Kelly ian.g.kelly at
Tue Feb 14 18:37:35 CET 2012

On Tue, Feb 14, 2012 at 9:55 AM, Den <patentsvnc at> wrote:
> On Feb 14, 8:22 am, Arnaud Delobelle <arno... at> wrote:
>> On 14 February 2012 15:31, Dennis Lee Bieber <wlfr... at> wrote:
>> > On Tue, 14 Feb 2012 16:01:05 +0100, Jabba Laci <jabba.l... at>
>> > wrote:
>> >>Could someone please tell me what the following sorting algorithm is called?
>> >>Let an array contain the elements a_1, a_2, ..., a_N. Then:
>> >>for i = 1 to N-1:
>> >>    for j = i+1 to N:
>> >>        if a_j < a_i then swap(a_j, a_i)
>> >        Off hand... The ancient Bubble-Sort...
>> >
>> Ahem...
>> No, it's not Bubble Sort.  Bubble sort only swaps adjacent terms.
>> I don't know what this sort is called, if it even has a name.  It's a
>> kind of Selection Sort, as each pass it looks for the minimum of the
>> remaining unsorted items.  But it ruffles the unsorted list each pass,
>> seemingly to save using an extra register to store the current minumum
>> (there was a time when registers were at a premium).
>> --
>> Arnaud
> I disagree.  In a bubble sort, one pointer points to the top element,
> while another descents through all the other elements, swapping the
> elements at the pointers when necessary.  Then the one pointer moved
> down to the next element and the process repeats.  This looks like the
> bubble sort to me.  It was one of the first algorithms I had to
> program in my first programming class in 1969.

Either you're misremembering, or the algorithm you programmed 43 years
ago was not actually bubble sort.  Quoting from Wikipedia:

Bubble sort, also known as sinking sort, is a simple sorting algorithm
that works by repeatedly stepping through the list to be sorted,
comparing each pair of adjacent items and swapping them if they are in
the wrong order. The pass through the list is repeated until no swaps
are needed, which indicates that the list is sorted. The algorithm
gets its name from the way smaller elements "bubble" to the top of the

In the present algorithm, you'll note that elements in the unsorted
part of the list do not "bubble up" as they would in bubble sort.
Rather, they jump around somewhat randomly until they are finally
selected for the current sort index.  I agree with Arnaud -- this is a
selection sort variant that saves a local variable (the index of the
minimum element) by placing it at the current sort index instead -- at
the cost of doing additional swaps.  Probably not a good trade-off in
Python (but then again, no pure Python sort algorithm is likely to
perform better than the built-in).


More information about the Python-list mailing list