# Another basic question about sorting w/o the sort method

**URL:** <https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221>\
**Category:** Ruby\
**Created:** [October 15, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221 "2006-10-15T19:46:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/1 "2006-10-15T19:46:54Z")

</div>

Hi,

I worked through the exercise in Chris P.'s tutorial where you sort  
an array without using the sort method  
([Arrays and Iterators - Learn to Program](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  
[http://groups.google.com/group/comp.lang.ruby/tree/browse\_frm/thread/410b0ac3d37b2c5e/67afd36eba0bd5ec?rnum=1&q=sort+pine+exercise&\_done=%2Fgroup%2Fcomp.lang.ruby%2Fbrowse\_frm%2Fthread%2F410b0ac3d37b2c5e%2F35a2dc683df509a7%3Flnk%3Dgst%26q%3Dsort+pine+exercise%26rnum%3D2%26#doc\_35a2dc683df509a7](http://groups.google.com/group/comp.lang.ruby/tree/browse_frm/thread/410b0ac3d37b2c5e/67afd36eba0bd5ec?rnum=1&q=sort+pine+exercise&_done=%2Fgroup%2Fcomp.lang.ruby%2Fbrowse_frm%2Fthread%2F410b0ac3d37b2c5e%2F35a2dc683df509a7%3Flnk%3Dgst%26q%3Dsort+pine+exercise%26rnum%3D2%26#doc_35a2dc683df509a7))

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? And is that formula of a[j], … = … a  
simple equation you can use in many different ways or a more specific  
method?

I tried to use only what I had learned in the tutorial up to the point  
of the exercise, though I admit I flaked and looked up what the delete  
method was. Here’s what I wrote:

# declare variables and arrays

word = ‘start’  
unsortedList = []  
sortedList = []  
lowercase = []  
trues = 0

# say hello

puts 'Hello. Type a word, press [ENTER] after every word, [ENTER] when  
done: ’

# get the input

while word != ‘’  
word = gets.chomp  
unsortedList.push word  
end

# downcase the list so we can sort it

unsortedList.each do |i|  
lowercase.push i.downcase  
end

# sort the list

while lowercase.length \> 0

lowercase.each do |i|

```
lowercase.each do |j|

  if (i <= j)
  trues = trues + 1
  end

end

if (trues == lowercase.length)
sortedList.push i
lowercase.delete(i)
trues = 0
else
trues = 0
end

```

end

end

# see the sorted list

puts ‘-’ \* 60  
puts ‘Your sorted list, dude:’  
puts sortedList

# done

Needless to say this looks much more clunky to me though it was still a  
lot of fun to do. If anyone can help with some of my questions or  
comment on my solution that would be awesome,

thanks, George

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/2 "2006-10-15T20:47:00Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> puts a.to_s
> 
> ```
> 
> 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
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/3 "2006-10-15T22:41:12Z")

</div>

Thanks Robert, that’s a good explanation. In fact I didn’t realize  
bubblesort is a general term and not just a name for the method (?) of  
the solution I quoted. Reading about bubblesort and all those other  
sorting algorithms referenced via bubblesort has been very  
illuminating!

thanks again, George

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/4 "2006-10-15T23:17:15Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

I do. 🙂 m.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/5 "2006-10-16T14:02:26Z")

</div>

On 15.10.2006 22:36, removed\_email\_address@domain.invalid wrote:

> Thanks Robert, that’s a good explanation. In fact I didn’t realize  
> bubblesort is a general term and not just a name for the method (?) of  
> the solution I quoted. Reading about bubblesort and all those other  
> sorting algorithms referenced via bubblesort has been very  
> illuminating!

You’re welcome. Also, I just realize there is a related discussion in  
/.:

> **[Advanced Data Structures? - Slashdot](https://slashdot.org/story/06/10/05/0435200/advanced-data-structures)**
>
> mdf356 asks: "It's been 5 years since I left graduate school and started designing and writing software for a living. After 5 years of writing operating systems code, I feel like I've forgotten some of the more advanced data structures I used to...

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/6 "2006-10-16T16:15:43Z")

</div>

Robert K. wrote:

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

FWIW, Lua supports parallel assignment:  
Lua 5.1 Copyright (C) 1994-2006 [Lua.org](http://Lua.org), PUC-Rio

> a=1;b=2  
> a,b=b,a  
> =a  
> 2  
> =b  
> 1  
> numbers = { 1, 2, 3, 4, 5 }  
> a,b,c = unpack( numbers )  
> print( a, b, c )  
> 1 2 3

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/another-basic-question-about-sorting-w-o-the-sort-method/72221/7 "2006-10-16T16:23:47Z")

</div>

> Robert K. wrote:
> 
> > 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.
> 
> FWIW, Lua supports parallel assignment:

So does Python…

Regards,  
Rimantas
