```georgeoliverGO / gmail.com wrote:
> Hi,
>
> I worked through the exercise in Chris Pine's tutorial where you sort
> an array without using the sort method
> (http://pine.fm/LearnToProgram/?Chapter=07), and I got a working
> solution, but I'm curious about how to improve it. Can anyone offer
> some tips? I've looked at a couple of solutions here like this one:
>
>    def bubblesort(a)
>         (a.length-1).downto(0) do |i|
>             puts a.to_s
>             0.upto(i-1) do |j|
>                 if a[j] > a[j+1]
>                     a[j], a[j+1] = a[j+1], a[j] # swap
>                 end
>             end
>         end
>     end
>
> (from
>
> This is pretty much over my head but it looks a lot cooler. I would
> like to write this way. What I don't understand about the above is
> this:
>
>                 if a[j] > a[j+1]
>                     a[j], a[j+1] = a[j+1], a[j] # swap
>
> I'm reading this to mean if the jth position in a is lexically greater
> than the jth + 1 position, then those two positions switch places, for
> example [z, m, p, a] becomes [ m, p, a, z], but then how does 'a' get
> to the top of the array?

Because the process is repeated n-1 times.  For better explanation I
suggest googling for "bubble sort" and / or to buy a book on data
structures and algorithms.  IMHO this belongs on every software
developer's desk anyway because the topic is so fundamental.

> And is that formula of a[j], ... = ... a
> simple equation you can use in many different ways or a more specific
> method?

No, it's the normal assignment operator - if you have multiple values
left and right it's also called "parallel assignment" IIRC.  I do not
know another programming language that has this feature.  Basically it
ensures that all right hand sides are evaluated before the assignment
takes place.  This allows to swap values like in

a,b=b,a

Kind regards

robert

```