2009/12/29 Marnen Laibow-Koser <marnen / marnen.org>:
> Joe User wrote:

Somehow I believe I heard that name somewhere already... :-)

>> I'm working through the Learning To Program book(2nd edition) and am
>> stuck on the sort/recursion exercise (page 93/94). The exercise is to
>> roll your own sort method for use on an array. =A0The suggestions are to
>> create an additional two arrays; 1) for the sorted words 2) another
>> for the unsorted words, take the list of words, find the smallest, and
>> put it on the end of the already-sorted list. =A0The remaining items go =
on
>> the
>> unsorted. =A0The idea being to recurse through the list of unsorted word=
s
>> adding elements to the sorted list until done.
>
> In Ruby, this is frankly stupid. =A0No Ruby programmer would do this in
> any other way than by using the .sort method, providing a comparator
> block, and letting Ruby take care of the bookkeeping.

I don't see how that is a stupid exercise.  I reckon this is not
mainly an exercise to introduce sorting but rather to get accustomed
with class Array and probably algorithms in general.

>> There are several routes I could take to get closer but I'm trying to
>> stay within the constraints of the book. =A0That means I can use .each,
>> .push, .pop, .length, .join, and .last. =A0Hopefully I'm not leaving
>> anything out. =A0Certainly I could use the .sort method and I could look
>> at the answer in the book but the idea is creating my own for the
>> experience. =A0But I'm stuck. =A0I'm hoping for a nudge in the right
>> direction instead of someone telling me outright
>> how to do it.
>>
>> In English what I think I need to do is take each element of the array
>> and compare it to the rest of the elements and if it's the smallest,
>> push it onto the sorted list and push the remaining elements onto the
>> unsorted. =A0Then call the method again with the sorted and unsorted
>> lists. That's the recursion part.
>
> That would certainly be a way of doing it. =A0Have you written automated
> tests to encapsulate these requirements? =A0If not, do so before writing
> another line of code.

Maybe it's a bit early to start writing tests when someone is starting
to learn to program...  When I started learning software development I
certainly got more kick out of a working program than a working unit
test before hand. :-)

>> I guess the problem I'm having is knowing how to check each element of
>> the unsorted array against the other elements. =A0I can check if the
>> current element is smaller than the other elements using < (less than)
>> but that doesn't mean it's the smallest in the whole array.
>
> Sure it does. =A0(Of course, this is quite computationally inefficient.)

As I said, I don't believe this is the point of the exercise.

"Joe", a hint for your "finding the smallest" rock that you are stuck
on: you need only a single pass on the Array to find the smallest (or
largest) element.  Just start with an empty "current smallest",
iterate through the Array (using #each for example) and compare the
"current smallest" with the "current iterated".  You know what you
have to do if you find an element that is smaller than the "current
smallest"...  You can then remove it from the unsorted Array by using
#delete_at or #delete, append it to the sorted and repeat.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/