From: Matthew Smillie Date: 2006-06-29T09:24:19+09:00 Subject: Re: Sorting arrays On Jun 28, 2006, at 23:59, Dark Ambient wrote: > So I start by collecting a group of words which get put into the > list[] array. > The task is to compare the words in list[] using < operator. The ones > that are true go into the "sorted array", the ones that fail go into > the "unsorted array". Compare a word to which other word? Their neighbours? The first element in list? Random other words? Are you certain you know exactly what has to be done? You won't be able to program it if you can't describe it. Maybe posting the actual text of the assignment? Out of curiosity, *is* this for some sort of coursework? Or do you just like setting yourself obscure problems? > The next step is to use the same operation but between the sorted and > unsorted arrays, till all is sorted in (yep) the sorted array. I've > been every which way and Sunday on this but so far have had little > luck and plenty of strange results. Trying to peer through the confusion over what you're actually trying to do, it really, really does sound like insertion sort, simply with separate arrays for the sorted and unsorted portions of the input (possibly to keep people from just grabbing a simple answer from the web?) Looking at your code, one thing stands out: if you push onto the sorted array simply when x < y for some y in the middle of the sorted array, the sorted array will no longer be sorted (unless y happens to be the last element of the sorted array or equal to it). You definitely want some sort of insertion there rather than a push. The push there will end up simply appending the unsorted array to the sorted one, since it always adds the unsorted word to the end of the array. I'll give you a hint that might help your experimentation: at no point should your sorted array ever be unsorted. If you code this test right in, you'll at least know when you're on the wrong track. class Array def sorted? self == self.sort end end Using that, you can do a check to see if the array is still in sorted order when you change it. If it's ever not in sorted order, you've messed up. Example: # add x to the array, but not with push, right? unless sorted.sorted? puts "sorted array in unsorted order after adding #{x}!" puts sorted exit end matthew smillie.