From: Marnen Laibow-Koser Date: 2009-12-30T01:47:43+09:00 Subject: Re: Creating my own method for sorting an array Robert Klemme wrote: > 2009/12/29 Marnen Laibow-Koser : >> Joe User wrote: > > Somehow I believe I heard that name somewhere already... :-) > >> In Ruby, this is frankly stupid. �No 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. Then they probably should have picked a better example, or at least explained common sort algorithms before turning you loose v > >>> 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. �Then call the method again with the sorted and unsorted >>> lists. That's the recursion part. >> >> That would certainly be a way of doing it. �Have you written automated >> tests to encapsulate these requirements? �If 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... No. Testing is a fundamental part of programming, though it is often neglected. > When I started learning software development I > certainly got more kick out of a working program than a working unit > test before hand. :-) It's the working test beforehand that lets you know when you have a working program. It really is best to write the tests first. Read up on test-first development and apply it religiously -- it really is worth it. > >>> I guess the problem I'm having is knowing how to check each element of >>> the unsorted array against the other elements. �I 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. �(Of course, this is quite computationally inefficient.) > > As I said, I don't believe this is the point of the exercise. > Perhaps not. And it may be a good exercise for learning algorithmic concepts. But really, it teaches you nothing good about writing idiomatic, efficient Ruby. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.