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


Groups > comp.programming > #1336

Re: What algorithm is this? (Variant of Selection sort?)

Message-ID <4F45B7F2.2BE5@mindspring.com> (permalink)
Date 2012-02-22 22:52 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.programming
Subject Re: What algorithm is this? (Variant of Selection sort?)
References (1 earlier) <slrnjjhg04.ljt.mordor@fly.srk.fer.hr> <1bbf76cc-241c-4936-bf52-3d48e1180c41@kn4g2000pbc.googlegroups.com> <4F3A7F10.6F65@mindspring.com> <a8288374-8ff2-44bc-b1a7-3fe61381173a@og8g2000pbb.googlegroups.com> <4F3B5380.F0B@mindspring.com>

Show all headers | View raw


pete wrote:
> 
> R. Rajesh Jeba Anbiah wrote:
> >
> > On Feb 14, 8:34 pm, pete <pfil...@mindspring.com> wrote:
> > > R. Rajesh Jeba Anbiah wrote:
> > > > On Feb 13, 12:47 pm, Zeljko Vrba <mordor.nos...@fly.srk.fer.hr> wrote:
> > > > > On 2012-02-13, R. Rajesh Jeba Anbiah <ng4rrjanb...@rediffmail.com> wrote:
> > > > > ><?php
> > > > > > $arr = array(10,9,8,7,6,5,4,3,2,1);
> > > > > > for($i=0, $len=count($arr); $i<$len-1; ++$i){
> > > > > >   for($j=$i+1, $len=count($arr); $j<$len; ++$j){
> > > > > >      if ($arr[$i] > $arr[$j]) {
> > > > > >         $temp = $arr[$i];
> > > > > >         $arr[$i] = $arr[$j];
> > > > > >         $arr[$j] = $temp;
> > > > > >      }
> > > > > >   }
> > > > > > }
> > > > > > ?>
> > >
> > > > > >    As I understand, selection sort is
> > > > > > about finding index first and
> > > > > > doing the swap. What about the above? Is it a variant or sort with
> > > > > > some name? TIA
> > >
> > > > > Looks like textbook implementation of Bubble sort.
> > >
> > > >    Yes, found in textbook only. But, it's not a Bubble sort. Right?
> > > >http://en.wikipedia.org/wiki/Bubble_sort
> > >
> > > It looks a lot like bubble sort,
> > > but it is not bubble sort.
> > >
> > > Bubble sort works by swapping adjacent elements.
> > > The algorithm which you have shown,
> > > swaps elements which are not adjacent.
> > >
> > > I would just call your algorithm "simple selection sort",
> > > while realizing that there are variations
> > > on the way that it can be implemented.
> > >
> > > Selection sorts, sort from one end to the other.
> > >
> > > Knuth classifies heapsort
> > > as belonging to the selection sort family of algorithms.
> > > As heapsort is typically implementd for sorting arrays:
> > > the last element is sorted first,
> > > and then the next to last,
> > > and so on.
> >
> >     Thanks for your valuable feedback. On a personal note I'm thinking
> > why it's not officially documented and named--even though it's
> > available in some textbooks as "bubble sort".
> 
> That is definitely not bubble sort.
> 
> This is bubble sort:
> 
> $arr = array(10,9,8,7,6,5,4,3,2,1);
> for($i=0, $len=count($arr); $i<$len-1; ++$i){
>   for($j=$len-1, $len=count($arr); $j>$i; --$j){
>      if ($arr[$j-1] > $arr[$j]) {
>         $temp = $arr[$j-1];
>         $arr[$j-1] = $arr[$j];
>         $arr[$j] = $temp;
>      }
>   }
> }
> 
> Bubble sort is a stable sort,
> simple selection sort is not.

One reason that there might not be too much
literature on this particular variety of simple selection sort,
is that it is not very good.

If one were desperate to find a metric 
by which simple selection sort 
could be said to be better than bubble sort, 
you could say that ordinary simple selection sort
does (n - 1) swaps for worst case, 
while bubble sort does (((n - 1) * n) / 2) swaps for worst case.

But this particular variety of selection sort
does (((n - 1) * n) / 2) swaps for worst case
and it does (((n - 1) * n) / 2) comparisons for every case,
which means that by any metric,
its not as good as bubble sort.

And bubble sort is not really a very good algorithm.

-- 
pete

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

What algorithm is this? (Variant of Selection sort?) "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@rediffmail.com> - 2012-02-12 23:36 -0800
  Re: What algorithm is this? (Variant of Selection sort?) Zeljko Vrba <mordor.nospam@fly.srk.fer.hr> - 2012-02-13 07:47 +0000
    Re: What algorithm is this? (Variant of Selection sort?) "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@rediffmail.com> - 2012-02-12 23:55 -0800
      Re: What algorithm is this? (Variant of Selection sort?) pete <pfiland@mindspring.com> - 2012-02-14 10:34 -0500
        Re: What algorithm is this? (Variant of Selection sort?) "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@rediffmail.com> - 2012-02-14 21:04 -0800
          Re: What algorithm is this? (Variant of Selection sort?) pete <pfiland@mindspring.com> - 2012-02-15 01:41 -0500
            Re: What algorithm is this? (Variant of Selection sort?) pete <pfiland@mindspring.com> - 2012-02-22 22:52 -0500
          Re: What algorithm is this? (Variant of Selection sort?) Patricia Shanahan <pats@acm.org> - 2012-02-15 09:45 -0800
  Re: What algorithm is this? (Variant of Selection sort?) Robert Wessel <robertwessel2@yahoo.com> - 2012-02-13 03:59 -0600
    Re: What algorithm is this? (Variant of Selection sort?) "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@rediffmail.com> - 2012-02-13 02:34 -0800

csiph-web