name of a sorting algorithm

Mel Wilson mwilson at
Tue Feb 14 17:44:20 CET 2012

Prasad, Ramit wrote:

> for i in xrange (N-1):
>     for j in xrange (i, N):
>         if a[j] < a[i]:
>             a[i], a[j] = a[j], a[i]
>> It's what Wikipedia says a selection sort is: put the least element in
>> [0], the least of the remaining elements in [1], etc.
> If your only requirement to match to selection sort is the end result,
> then every sort would be selection sort. If you meant "put the least
> element in [0] in the first pass" then that would indeed be selection
> sort, but that is not what the above code does. The above code is bubble
> sort.

Well, the classic bubble sort swaps adjacent elements until the extreme one 
gets all the way to the end.  This sort continually swaps with the end 
element during one pass until the end element holds the extreme.  Then it 
shrinks the range and swaps then next less extreme into the new end element.  
It does extra swaps because it combines the swap operation with recording 
the temporary extreme while it searches the subrange.


More information about the Python-list mailing list