Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20414

Re: name of a sorting algorithm

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jabba.laci@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'one?': 0.05; '"""': 0.07; 'elements.': 0.07; 'variant': 0.07; 'python': 0.08; 'bubble': 0.09; 'indicates': 0.09; 'to:name:python mailing list': 0.09; 'algorithm': 0.13; 'adjacent': 0.16; 'naive': 0.16; 'side.': 0.16; 'sorted,': 0.16; 'swaps': 0.16; 'repeated': 0.18; 'thus': 0.21; '(but': 0.21; 'header:In-Reply-To:1': 0.22; 'end,': 0.23; 'indexing': 0.23; 'pair': 0.23; 'works.': 0.23; 'starts': 0.24; 'elements': 0.24; 'index': 0.24; 'variable': 0.28; 'needed,': 0.28; 'order.': 0.28; 'repeatedly': 0.28; 'pass': 0.29; 'message- id:@mail.gmail.com': 0.29; 'version.': 0.29; 'url:wiki': 0.29; 'array': 0.30; '(the': 0.30; 'ago': 0.31; 'actually': 0.31; 'pure': 0.32; 'does': 0.32; 'list': 0.32; 'sort': 0.33; 'instead': 0.33; 'agree': 0.33; 'hi,': 0.34; 'jump': 0.34; 'to:addr:python- list': 0.35; 'list.': 0.35; '(for': 0.35; 'received:209.85.160': 0.36; 'largest': 0.36; 'two': 0.36; 'comparing': 0.37; 'element': 0.37; 'saves': 0.37; 'but': 0.37; 'shows': 0.37; 'received:google.com': 0.37; 'either': 0.37; 'received:209.85': 0.38; '2nd': 0.38; 'somewhat': 0.38; 'doing': 0.38; 'received:209.85.160.46': 0.39; 'received:mail- pw0-f46.google.com': 0.39; 'smaller': 0.39; 'url:org': 0.39; 'url:en': 0.39; 'received:209': 0.39; 'called': 0.40; 'to:addr:python.org': 0.40; 'selection': 0.40; "you'll": 0.61; 'simple': 0.61; 'maximum': 0.66; 'selected': 0.66; 'subject:name': 0.67; '1st': 0.70; 'placing': 0.74; 'algorithm,': 0.84; 'gif': 0.84; 'sort.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=m7pXFgEl4+DJ/bMK+tE2zmp+AD5OeB/e49xXMuty9jg=; b=iQgx4LRvkM0imX0tJoGh/cHIurn1htO6qdHHflcUjHMyEqDzR7J6ow8ocjXgERq6ai Fo8vnAoePOnTipTSCbaPm1QhqO1X/Z0UJT2tZ9Bgjdf5ccEgT/CnV0vmXUWE+1xJvQCf c80BEmP9V+TQ0p50lrpQcJYlBX2TWSVM6vzMQ=
MIME-Version 1.0
In-Reply-To <CALwzidkaPaGFxjzG93SdL+pJNJb4LNiX=BUZXa+N6XoqVhQ8-w@mail.gmail.com>
References <CAOuJsMnV0GPK=Jm=s5K7QRS1VO=wy62-H7FfaB-Y5QBitRDu=Q@mail.gmail.com> <o9ukj7p7f6h3sn6p6d0hpoh09vd9rpssu8@4ax.com> <mailman.5805.1329236568.27778.python-list@python.org> <73b8d112-9e94-486e-b06c-bdeebc0bf964@q12g2000yqg.googlegroups.com> <CALwzidkaPaGFxjzG93SdL+pJNJb4LNiX=BUZXa+N6XoqVhQ8-w@mail.gmail.com>
From Jabba Laci <jabba.laci@gmail.com>
Date Tue, 14 Feb 2012 19:10:35 +0100
Subject Re: name of a sorting algorithm
To Python mailing list <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5809.1329243057.27778.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1329243057 news.xs4all.nl 6980 [2001:888:2000:d::a6]:44901
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20414

Show key headers only | View raw


Hi,

> 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
> list.
> """

I don't agree with the last sentence. During bubble sort, in the 1st
pass the largest element is moved to the top (for me "top" means the
right side (end) of an array). Thus the end of the array is sorted. In
the 2nd pass, the largest element of the unsorted left part is moved
to the end, etc. That is, it's the _larger_ elements that bubble to
the top. At http://en.wikipedia.org/wiki/Bubble_sort you can find an
animated gif that shows how the algorithm works.

> 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).

The minimum selection sort is an improvement of this "noname"
algorithm. I give it in pseudo-code. Let A be an array with N
elements. Indexing starts with 1.

for i := 1 to N-1:
    minindex := i
    for j := i+1 to N:
        if A[j] < A[minindex] then minindex := j
    end for
    if i != minindex then swap(A[i], A[minindex])
end for

The two loops are the same as in the naive version. It will also sort
the array from the left side. It does much less swaps than the naive
version.

If the "noname" algorithm is called "selection sort", then its name
can be misleading. One may ask "OK, but which one? Minimum or maximum
selection sort?". Well, neither...

Laszlo

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: name of a sorting algorithm Arnaud Delobelle <arnodel@gmail.com> - 2012-02-14 16:22 +0000
  Re: name of a sorting algorithm Den <patentsvnc@gmail.com> - 2012-02-14 08:55 -0800
    Re: name of a sorting algorithm Mel Wilson <mwilson@the-wire.com> - 2012-02-14 12:04 -0500
    Re: name of a sorting algorithm Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-14 10:37 -0700
    Re: name of a sorting algorithm Jabba Laci <jabba.laci@gmail.com> - 2012-02-14 19:10 +0100
    Re: name of a sorting algorithm Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-14 13:59 -0700

csiph-web